Changing the time series frequency: resampling

Manipulating Time Series Data in Python

Stefan Jansen

Founder & Lead Data Scientist at Applied Artificial Intelligence

Changing the frequency: resampling

  • DateTimeIndex: set & change freq using .asfreq()
  • But frequency conversion affects the data
    • Upsampling: fill or interpolate missing data
    • Downsampling: aggregate existing data
  • pandas API:
    • .asfreq(), .reindex()
    • .resample() + transformation method
Manipulating Time Series Data in Python

Getting started: quarterly data

dates = pd.date_range(start='2016', periods=4, freq='Q')

data = range(1, 5)
quarterly = pd.Series(data=data, index=dates)
quarterly
2016-03-31    1
2016-06-30    2
2016-09-30    3
2016-12-31    4
Freq: Q-DEC, dtype: int64 # Default: year-end quarters
Manipulating Time Series Data in Python

Upsampling: quarter => month

monthly = quarterly.asfreq('M') # to month-end frequency
2016-03-31    1.0
2016-04-30    NaN
2016-05-31    NaN
2016-06-30    2.0
2016-07-31    NaN
2016-08-31    NaN
2016-09-30    3.0
2016-10-31    NaN
2016-11-30    NaN
2016-12-31    4.0
Freq: M, dtype: float64
  • Upsampling creates missing values
monthly = monthly.to_frame('baseline') # to DataFrame
Manipulating Time Series Data in Python

Upsampling: fill methods

monthly['ffill'] = quarterly.asfreq('M', method='ffill')

monthly['bfill'] = quarterly.asfreq('M', method='bfill')
monthly['value'] = quarterly.asfreq('M', fill_value=0)
Manipulating Time Series Data in Python

Upsampling: fill methods

  • bfill: backfill
  • ffill: forward fill
            baseline  ffill  bfill  value
2016-03-31       1.0      1      1      1
2016-04-30       NaN      1      2      0
2016-05-31       NaN      1      2      0
2016-06-30       2.0      2      2      2
2016-07-31       NaN      2      3      0
2016-08-31       NaN      2      3      0
2016-09-30       3.0      3      3      3
2016-10-31       NaN      3      4      0
2016-11-30       NaN      3      4      0
2016-12-31       4.0      4      4      4
Manipulating Time Series Data in Python

Add missing months: .reindex()

dates = pd.date_range(start='2016', 
                      periods=12, 
                      freq='M')
DatetimeIndex(['2016-01-31', 
               '2016-02-29', 
               ..., 
               '2016-11-30', 
               '2016-12-31'],
        dtype='datetime64[ns]', freq='M')
  • .reindex():
    • conform DataFrame to new index
    • same filling logic as .asfreq()
quarterly.reindex(dates)
2016-01-31    NaN
2016-02-29    NaN
2016-03-31    1.0
2016-04-30    NaN
2016-05-31    NaN
2016-06-30    2.0
2016-07-31    NaN
2016-08-31    NaN
2016-09-30    3.0
2016-10-31    NaN
2016-11-30    NaN
2016-12-31    4.0
Manipulating Time Series Data in Python

Let's practice!

Manipulating Time Series Data in Python

Preparing Video For Download...