Python Pandas - Get the quarter of the year from Period object

To get the quarter of the year component of a Period object, use the period.quarter property. This property returns an integer from 1 to 4 representing which quarter the period falls into.

Creating Period Objects

First, let's import pandas and create Period objects ?

import pandas as pd

# Create Period objects with different formats
period1 = pd.Period("2020-02-27 08:32:48")
period2 = pd.Period(freq="M", year=2021, month=8, day=16, hour=2, minute=35)

print("Period1:", period1)
print("Period2:", period2)
Period1: 2020-02-27 08:32:48
Period2: 2021-08

Getting Quarter Information

Use the .quarter property to extract quarter information from Period objects ?

import pandas as pd

period1 = pd.Period("2020-02-27 08:32:48")
period2 = pd.Period(freq="M", year=2021, month=8, day=16, hour=2, minute=35)

# Get quarters
quarter1 = period1.quarter
quarter2 = period2.quarter

print("Quarter from Period1 (February):", quarter1)
print("Quarter from Period2 (August):", quarter2)
Quarter from Period1 (February): 1
Quarter from Period2 (August): 3

Quarter Definitions

The quarters are defined as follows:

  • Quarter 1: January 1 to March 31
  • Quarter 2: April 1 to June 30
  • Quarter 3: July 1 to September 30
  • Quarter 4: October 1 to December 31

Multiple Periods Example

Here's an example with periods from different quarters ?

import pandas as pd

# Create periods from different quarters
periods = [
    pd.Period("2023-01-15"),  # Q1
    pd.Period("2023-05-20"),  # Q2
    pd.Period("2023-09-10"),  # Q3
    pd.Period("2023-12-25")   # Q4
]

for i, period in enumerate(periods, 1):
    print(f"Period {i}: {period} ? Quarter {period.quarter}")
Period 1: 2023-01-15 ? Quarter 1
Period 2: 2023-05-20 ? Quarter 2
Period 3: 2023-09-10 ? Quarter 3
Period 4: 2023-12-25 ? Quarter 4

Conclusion

The quarter property provides a simple way to extract quarter information from pandas Period objects. It returns integers 1-4 corresponding to the four quarters of the year based on the period's month component.

---
Updated on: 2026-03-26T16:29:21+05:30

551 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements