Resampling time series data
In this tutorial, we will learn how to resample time series with Pandas.
How to do it...
We will download the daily price time series data for AAPL, and resample it to monthly data by computing the mean. We will accomplish this by creating a Pandas DataFrame
, and calling its
resample
method.
Creating a date-time index.
Before we can create a Pandas
DataFrame
, we need to create aDatetimeIndex
method to pass to theDataFrame
constructor. Create the index from the downloaded quotes data as follows:dt_idx = pandas.DatetimeIndex(quotes.date)
Creating the data frame.
Once we have the date-time index, we can use it together with the close prices to create a data frame:
df = pandas.DataFrame(quotes.close, index=dt_idx, columns=[symbol])
Resample.
Resample the time series to monthly frequency, by computing the mean:
resampled = df.resample('M', how=numpy.mean) print resampled
The resampled time series, as shown in the following, has one value for each month:
...