O'Reilly logo

NumPy Cookbook - Second Edition by Ivan Idris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Resampling time series data

In this tutorial, you 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 into monthly data by computing the mean. We will do this by creating a pandas DataFrame and calling its resample() method:

  1. Before we can create a pandas DataFrame, we need to create a DatetimeIndex object to pass to the DataFrame constructor. Create the index from the downloaded quotes data, as follows:
    dt_idx = pandas.DatetimeIndex(quotes.date)
  2. Once we have the date-time index, we use it together with the close prices to create a data frame:
    df = pandas.DataFrame (quotes.close, index=dt_idx, columns=[symbol])
  3. Resample the time series to monthly frequency by computing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required