12 Converting between Representations

Timestamped data can be converted to PeriodIndex-ed data using to_period and vice-versa using to_timestamp:

In [1]: rng = pd.date_range('1/1/2012', periods=5, freq='M')

In [2]: ts = pd.Series(np.random.randn(len(rng)), index=rng)

In [3]: ts
Out[3]: 
2012-01-31    0.469112
2012-02-29   -0.282863
2012-03-31   -1.509059
2012-04-30   -1.135632
2012-05-31    1.212112
Freq: M, dtype: float64

In [4]: ps = ts.to_period()

In [5]: ps
Out[5]: 
2012-01    0.469112
2012-02   -0.282863
2012-03   -1.509059
2012-04   -1.135632
2012-05    1.212112
Freq: M, dtype: float64

In [6]: ps.to_timestamp()
Out[6]: 
2012-01-01    0.469112
2012-02-01   -0.282863
2012-03-01   -1.509059
2012-04-01   -1.135632
2012-05-01    1.212112
Freq: MS, dtype: float64

Remember that ‘s’ and ‘e’ can be used to return the timestamps at the start or end of the period:

In [7]: ps.to_timestamp('D', how='s')
Out[7]: 
2012-01-01    0.469112
2012-02-01   -0.282863
2012-03-01   -1.509059
2012-04-01   -1.135632
2012-05-01    1.212112
Freq: MS, dtype: float64

Converting between period and timestamp enables some convenient arithmetic functions to be used. In the following example, we convert a quarterly frequency with year ending in November to 9am of the end of the month following the quarter end:

In [8]: prng = pd.period_range('1990Q1', '2000Q4', freq='Q-NOV')

In [9]: ts = pd.Series(np.random.randn(len(prng)), prng)

In [10]: ts.index = (prng.asfreq('M', 'e') + 1).asfreq('H', 's') + 9

In [11]: ts.head()
Out[11]: 
1990-03-01 09:00   -0.173215
1990-06-01 09:00    0.119209
1990-09-01 09:00   -1.044236
1990-12-01 09:00   -0.861849
1991-03-01 09:00   -2.104569
Freq: H, dtype: float64