Format Period Object and Display Year Without Century in Python Pandas



To format the Period object, use the period.strftime() method and to display the year without century, set the parameter as %y.

At first, import the required libraries −

import pandas as pd

The pandas.Period represents a period of time. Creating a Period object

period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)

Display the Period object

print("Period...\n", period)

Display the result. Here, year is displayed without century

print("\nString representation (year without century)...\n", period.strftime('%y'))

Example

Following is the code

import pandas as pd

# The pandas.Period represents a period of time
# Creating a Period object
period = pd.Period(freq="S", year = 2021, month = 9, day = 18, hour = 8, minute = 20, second = 45)

# display the Period object
print("Period...\n", period)

# display the result
# Here, year is displayed without century
print("\nString representation (year without century)...\n", period.strftime('%y'))

Output

This will produce the following code

Period...
2021-09-18 08:20:45

String representation (year without century)...
21
Updated on: 2021-10-20T06:51:41+05:30

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements