Time series in pandas


In [9]:
import numpy as np
import pandas as pd
%matplotlib inline

In [10]:
rng = pd.date_range('1/1/2011', periods=72, freq='H')

In [11]:
rng.dtype


Out[11]:
dtype('<M8[ns]')

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

In [23]:
delta.head()


Out[23]:
2011-01-01 00:00:00   -1.065152
2011-01-01 01:00:00   -0.462886
2011-01-01 02:00:00   -1.004773
2011-01-01 03:00:00    1.521123
2011-01-01 04:00:00    1.756298
Freq: H, dtype: float64

In [21]:
ts = delta.cumsum()

In [24]:
ts.head()


Out[24]:
2011-01-01 00:00:00   -1.065152
2011-01-01 01:00:00   -1.528037
2011-01-01 02:00:00   -2.532810
2011-01-01 03:00:00   -1.011687
2011-01-01 04:00:00    0.744611
Freq: H, dtype: float64

In [22]:
ts.plot()


Out[22]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff96432f828>

Change frequency and fill gaps


In [25]:
conv = ts.asfreq('45Min', method='pad')

In [26]:
conv.head()


Out[26]:
2011-01-01 00:00:00   -1.065152
2011-01-01 00:45:00   -1.065152
2011-01-01 01:30:00   -1.528037
2011-01-01 02:15:00   -2.532810
2011-01-01 03:00:00   -1.011687
Freq: 45T, dtype: float64