Python Pandas - Format the Period object and display the Year without century

To format a Period object in Pandas, use the period.strftime() method. To display the year without century, use the %y format parameter which shows only the last two digits of the year.

Creating a Period Object

First, import pandas and create a Period object with specific datetime components ?

import pandas as pd

# Create a Period object with frequency 'S' (second)
period = pd.Period(freq="S", year=2021, month=9, day=18, hour=8, minute=20, second=45)
print("Period object:", period)
Period object: 2021-09-18 08:20:45

Formatting Year Without Century

Use the strftime() method with the %y format code to display only the last two digits of the year ?

import pandas as pd

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

# Format to show year without century
year_short = period.strftime('%y')
print("Year without century:", year_short)
Year without century: 21

Common Format Codes

Here are other useful format codes you can use with strftime() ?

import pandas as pd

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

print("Year with century:", period.strftime('%Y'))
print("Year without century:", period.strftime('%y'))
print("Month name:", period.strftime('%B'))
print("Day of month:", period.strftime('%d'))
print("Hour (24-hour):", period.strftime('%H'))
Year with century: 2021
Year without century: 21
Month name: September
Day of month: 18
Hour (24-hour): 08

Comparison

Format Code Description Example Output
%Y Year with century 2021
%y Year without century 21
%m Month as number 09
%B Full month name September

Conclusion

Use period.strftime('%y') to format a Pandas Period object and display the year without century. The strftime() method supports various format codes for customizing datetime string representations.

Updated on: 2026-03-26T17:45:58+05:30

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements