Resampling Time Series
Downsample daily data to monthly with resample('ME').sum() and upsample with forward fill to fill in missing intervals.
What Is Resampling?
Resampling changes the frequency of a time series. Downsampling reduces frequency — for example, converting daily data to monthly totals. Upsampling increases frequency — for example, converting monthly data to daily, filling gaps with interpolated values. Both operations require a DatetimeIndex and are performed with the resample() method, which is Pandas' time-aware version of groupby().
The resample() Method
df.resample(rule) creates a DatetimeIndexResampler object, where rule is a frequency offset alias. Like groupby(), nothing is computed until you chain an aggregation method. Common rules: 'D' (day), 'W' (week), 'ME' (month end), 'QE' (quarter end), 'YE' (year end). The DataFrame must have a DatetimeIndex.
import pandas as pd
import numpy as np
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=90, freq='D')
df = pd.DataFrame({'sales': np.random.randint(100, 500, 90)}, index=dates)
print(df.head())
print('Shape:', df.shape) # (90, 1) -- 90 daily rowsAll lessons in this course
- DatetimeIndex and Period Ranges
- Resampling Time Series
- Shifting and Lag Features
- Extracting Temporal Features