Frequency conversion of time-series data
The frequency of the data in a time-series can be converted in pandas using the .asfreq()
method of a Series
or DataFrame
. To demonstrate, we will use the following small subset of the MSFT stock closing values:
In [39]: sample = msftAC[:2] sample Out[39]: Date 2012-01-03 24.42183 2012-01-04 24.99657 Name: Adj Close, dtype: float64
We have extracted the first 2 days of adjusted close values. Let's suppose we want to resample this to have hourly sampling of data in-between the index labels. We can do this with the following command:
In [40]: sample.asfreq("H") Out[40]: 2012-01-03 00:00:00 24.42183 2012-01-03 01:00:00 NaN 2012-01-03 02:00:00 NaN ... 2012-01-03 22:00:00 NaN 2012-01-03 23:00:00 ...
Get Mastering pandas for Finance now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.