Python Pandas - Extract the quarter of the date from the DateTimeIndex with specific time series frequency

To extract the quarter of the date from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.quarter property.

Syntax

DateTimeIndex.quarter

This property returns an Int64Index containing the quarter of each date in the DateTimeIndex.

Quarter Mapping

The quarters are mapped as follows ?

  • Quarter 1 = January to March
  • Quarter 2 = April to June
  • Quarter 3 = July to September
  • Quarter 4 = October to December

Creating a DateTimeIndex

First, let's create a DateTimeIndex with specific frequency ?

import pandas as pd

# Create DatetimeIndex with period 6 and frequency as 2M (every 2 months)
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)

# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
DateTimeIndex...
DatetimeIndex(['2021-10-31 02:30:50+11:00', '2021-12-31 02:30:50+11:00',
               '2022-02-28 02:30:50+11:00', '2022-04-30 02:30:50+10:00',
               '2022-06-30 02:30:50+10:00', '2022-08-31 02:30:50+10:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='2M')

DateTimeIndex frequency...
<2 * MonthEnds>

Extracting Quarter Information

Now let's extract the quarter of each date using the quarter property ?

import pandas as pd

# Create DatetimeIndex with period 6 and frequency as 2M (every 2 months)
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')

# Get the quarter of each date
print("Quarter of each date:")
print(datetimeindex.quarter)
Quarter of each date:
Int64Index([4, 4, 1, 2, 2, 3], dtype='int64')

Understanding the Output

The output shows quarters for each date in the DateTimeIndex ?

  • 2021-10-31 ? Quarter 4 (October is in Q4)
  • 2021-12-31 ? Quarter 4 (December is in Q4)
  • 2022-02-28 ? Quarter 1 (February is in Q1)
  • 2022-04-30 ? Quarter 2 (April is in Q2)
  • 2022-06-30 ? Quarter 2 (June is in Q2)
  • 2022-08-31 ? Quarter 3 (August is in Q3)

Complete Example

import pandas as pd

# Create DatetimeIndex with period 6 and frequency as 2M (every 2 months)
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M')

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)

# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)

# Get the quarter of the date
print("\nQuarter of each date:")
print(datetimeindex.quarter)
DateTimeIndex...
DatetimeIndex(['2021-10-31 02:30:50+11:00', '2021-12-31 02:30:50+11:00',
               '2022-02-28 02:30:50+11:00', '2022-04-30 02:30:50+10:00',
               '2022-06-30 02:30:50+10:00', '2022-08-31 02:30:50+10:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='2M')

DateTimeIndex frequency...
<2 * MonthEnds>

Quarter of each date:
Int64Index([4, 4, 1, 2, 2, 3], dtype='int64')

Conclusion

The DateTimeIndex.quarter property provides a simple way to extract quarter information from datetime data. It returns an Int64Index with quarter values (1-4) corresponding to each date in the DateTimeIndex.

Updated on: 2026-03-26T17:08:10+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements