pythonpandasindexingsubsamplingpandas-timeindex

How to generate monthly period index with annual frequency?


How would generate in the most concise way a monthly period index that is observed only every 12 months?

I came up with the following solution

pd.period_range(start=pd.Period('1975-07'), 
                end=pd.Period('1985-07'),
                freq='M'
                ).to_frame().iloc[::12].index

but I was wondering if there is a way that avoids the conversion from period index to dataframe and back to period index.

This is the expected output:

PeriodIndex(['1975-07', '1976-07', '1977-07', '1978-07', '1979-07', '1980-07',
             '1981-07', '1982-07', '1983-07', '1984-07', '1985-07'],
            dtype='period[M]', freq='M')

Solution

  • Use freq='12M':

    pd.period_range(start=pd.Period('1975-07'), 
                    end=pd.Period('1985-07'),
                    freq='12M'
                    )