Skip to content
Advertisement

How to get strftime to output yearly quarters?

I am trying to create a list of year-quarters in python; per the documentation (https://pandas.pydata.org/docs/reference/api/pandas.Period.strftime.html), %q (I believe) should output an integer representation of calendar quarter, but instead the output I get is just %q literally.

Input:

pd.date_range('2021-01-01','2022-01-01',freq='Q').strftime('%Y-%q').tolist()

Expected Output:

['2021-01', '2021-02', '2021-03', '2021-04']

Actual Output:

['2021-%q', '2021-%q', '2021-%q', '2021-%q']

Advertisement

Answer

The %q formatter is specific to Period.strftime, so it must be used with Period data:

The method recognizes the same directives as the time.strftime function of the standard Python distribution, as well as the specific additional directives %f, %F, %q.

Either create a period_range directly (but note that it includes the end date, unlike date_range):

periods = pd.period_range('2021-01-01', '2022-01-01', freq='Q')
# PeriodIndex(['2021Q1', '2021Q2', '2021Q3', '2021Q4', '2022Q1'], dtype='period[Q-DEC]')

periods.strftime('%Y-0%q').tolist()
# ['2021-01', '2021-02', '2021-03', '2021-04', '2022-01']

Or convert an existing DatetimeIndex to a PeriodIndex:

dates = pd.date_range('2021-01-01', '2022-01-01', freq='Q')
# DatetimeIndex(['2021-03-31', '2021-06-30', '2021-09-30', '2021-12-31'], dtype='datetime64[ns]', freq='Q-DEC')

pd.PeriodIndex(dates).strftime('%Y-0%q').tolist()
# ['2021-01', '2021-02', '2021-03', '2021-04']
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement