A PeriodIndex function, which is an index type for a Period object, can be created in two ways:
- You can do it from a series of Period objects using the period_range function to create an analogue of date_range:
In [305]: perRng=pd.period_range('02/01/2014','02/06/2014',freq='D') perRng Out[305]: <class 'pandas.tseries.period.PeriodIndex'> freq: D [2014-02-01, ..., 2014-02-06] length: 6 In [306]: type(perRng[:2]) Out[306]: pandas.tseries.period.PeriodIndex In [307]: perRng[:2] Out[307]: <class 'pandas.tseries.period.PeriodIndex'> freq: D [2014-02-01, 2014-02-02]
As we can confirm from the preceding command, when you pull the covers, a PeriodIndex function is really an ndarray of Period objects.
- It can also be done through a direct call to the Period constructor:
In [312]: JulyPeriod=pd.PeriodIndex(['07/01/2014','07/31/2014'], freq='D') JulyPeriod Out[312]: <class 'pandas.tseries.period.PeriodIndex'> freq: D [2014-07-01, 2014-07-31]
The difference between the two approaches, as can be seen from the preceding output, is that period_range fills in the resulting ndarray, but the Period constructor does not, and you have to specify all the values that should be in the index.