Python Pandas PeriodIndex - Convert the PeriodArray to the specified frequency

To convert the PeriodArray to a specified frequency, use the periodIndex.asfreq() method. This method allows you to change the frequency of periods while preserving the time information.

Syntax

PeriodIndex.asfreq(freq, how='end')

Parameters

  • freq: The target frequency (e.g., 'M' for monthly, 'D' for daily)
  • how: How to handle conversion ('start' or 'end')

Creating a PeriodIndex

First, let's create a PeriodIndex object with yearly frequency ?

import pandas as pd

# Create a PeriodIndex object with yearly frequency
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
                              '2020-07-15 02:55:15', '2022-06-25 09:40:55'], freq="Y")

print("Original PeriodIndex:\n", periodIndex)
print("\nFrequency:", periodIndex.freqstr)
Original PeriodIndex:
 PeriodIndex(['2021', '2019', '2020', '2022'], dtype='period[A-DEC]')

Frequency: A-DEC

Converting to Monthly Frequency

Convert the yearly periods to monthly frequency using asfreq() ?

import pandas as pd

periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
                              '2020-07-15 02:55:15', '2022-06-25 09:40:55'], freq="Y")

# Convert to monthly frequency
monthly_periods = periodIndex.asfreq('M')
print("Converted to Monthly:\n", monthly_periods)
Converted to Monthly:
 PeriodIndex(['2021-12', '2019-12', '2020-12', '2022-12'], dtype='period[M]')

Converting to Different Frequencies

You can convert to various frequencies like daily, quarterly, or weekly ?

import pandas as pd

periodIndex = pd.PeriodIndex(['2021', '2020', '2019'], freq="Y")

print("Original (Yearly):", periodIndex)
print("To Quarterly:", periodIndex.asfreq('Q'))
print("To Monthly:", periodIndex.asfreq('M'))
print("To Daily:", periodIndex.asfreq('D'))
Original (Yearly): PeriodIndex(['2021', '2020', '2019'], dtype='period[A-DEC]')
To Quarterly: PeriodIndex(['2021Q4', '2020Q4', '2019Q4'], dtype='period[Q-DEC]')
To Monthly: PeriodIndex(['2021-12', '2020-12', '2019-12'], dtype='period[M]')
To Daily: PeriodIndex(['2021-12-31', '2020-12-31', '2019-12-31'], dtype='period[D]')

Using the 'how' Parameter

The how parameter controls whether to use the start or end of the period ?

import pandas as pd

periodIndex = pd.PeriodIndex(['2021'], freq="Y")

print("Original:", periodIndex)
print("End of year (default):", periodIndex.asfreq('D', how='end'))
print("Start of year:", periodIndex.asfreq('D', how='start'))
Original: PeriodIndex(['2021'], dtype='period[A-DEC]')
End of year (default): PeriodIndex(['2021-12-31'], dtype='period[D]')
Start of year: PeriodIndex(['2021-01-01'], dtype='period[D]')

Conclusion

The asfreq() method is essential for converting PeriodIndex objects between different time frequencies. Use the how parameter to control whether conversion uses the start or end of the period.

Updated on: 2026-03-26T18:03:45+05:30

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements