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:

JavaScript

Expected Output:

JavaScript

Actual Output:

JavaScript

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):

JavaScript

Or convert an existing DatetimeIndex to a PeriodIndex:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement