Python Pandas - Format the Period object and display Quarter


To format the Period object, use the period.strftime() method and to display Quarter, set the parameter as Q%q.

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, Period object is formatted and Quarter is displayed

print("\nString representation (display quarter)...\n", period.strftime('Q%q'))

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, Period object is formatted and Quarter is displayed
print("\nString representation (display quarter)...\n", period.strftime('Q%q'))

Output

This will produce the following code

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

String representation (display quarter)...
Q3

Updated on: 20-Oct-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements