Time Series Resampling (resample, asfreq)
Resampling changes the frequency of time-indexed data — turning daily readings into monthly totals (downsampling) or hourly readings into 15-minute slots (upsampling) by grouping rows into time buckets.
Learn Time Series Resampling (resample, asfreq) in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll learn df.resample() with .sum() , .mean() , and .ohlc() , how upsampling differs from downsampling, and when to reach for asfreq() instead.
Resampling is "groupby for time." When your index is a DatetimeIndex , calling df.resample("ME") slices the timeline into month-end buckets; you then chain an aggregation like .sum() or .mean() to collapse each bucket into one row. Going from a fine frequency (daily) to a coarse one (monthly) is called downsampling .
The rule argument is a frequency alias . The common ones are "D" (day), "W" (week), "ME" (month-end), "QE" (quarter-end), and "YE" (year-end). You can also prefix a number, e.g. "2D" for two-day windows.
Each bucket can be summarised in many ways. .ohlc() is the classic finance summary that returns four columns per bucket — open (first value), high (max), low (min), and close (last). When you want several statistics at once, pass a list to .agg([...]) .
Upsampling goes the other direction — from coarse to fine, e.g. daily to 12-hourly. Because the new timestamps did not exist in the original data, pandas fills them with NaN . You then decide how to fill: forward-fill with .ffill() , back-fill with .bfill() , or leave the holes for interpolation (the next lesson).
asfreq() is the lightweight cousin of resample. It does not bucket or aggregate — it simply lays a new fixed-frequency index over the data and keeps whatever value sits exactly on each new timestamp, inserting NaN elsewhere. Use it when the frequency is already regular and you just want to change its label rate.
❌ "Invalid frequency: M ... use 'ME' instead"
The old single-letter month alias was removed in modern pandas:
❌ "Only valid with DatetimeIndex" / TypeError
Calling resample when the dates are still a plain column:
You have website hits recorded every 6 hours. Downsample to daily numbers two ways.
Lesson complete — you can reshape time itself!
You can downsample with resample("ME"/"W"/"D") plus an aggregation, summarise buckets with .ohlc() and .agg([...]) , upsample with fills, and re-stamp a regular series with asfreq() .
🚀 Up next: Interpolation & Filling Gaps — smart ways to fill those upsampled NaNs.
Practice quiz
What must the index be for resample() to work?
- A datetime index
- A string index
- A range of integers
- A boolean index
Answer: A datetime index. resample() groups by time, so it needs a DatetimeIndex.
What does ts.resample('D') group the data into?
- Hourly buckets
- Daily buckets
- Weekly buckets
- Monthly buckets
Answer: Daily buckets. The 'D' frequency string means calendar-day buckets.
What must follow resample('2D') to get a result?
- A frequency string
- Nothing, it returns values
- An aggregation like .sum() or .mean()
- An index argument
Answer: An aggregation like .sum() or .mean(). resample returns a Resampler; you chain an aggregation such as .sum().
For daily values [1,2,3,4,5,6], what does resample('2D').sum() give?
Two-day groups sum to 1+2=3, 3+4=7, 5+6=11.
Which frequency string means month-end?
- 'ME'
- 'D'
- 'W'
- 'H'
Answer: 'ME'. 'ME' buckets data by month end (older pandas used 'M').
What does the 'W' frequency string resample by?
- By year
- By week
- By hour
- By minute
Answer: By week. 'W' groups the data into weekly buckets.
When upsampling to a finer frequency, how do you fill new gaps forward?
- .sum()
- .count()
- .diff()
- .ffill()
Answer: .ffill(). ffill() carries the last value forward into the new finer slots.
What does resample('2D').mean() compute for each bucket?
- The average of the values in that 2-day window
- The count of values
- The first value only
- The cumulative sum
Answer: The average of the values in that 2-day window. mean() averages the values falling in each 2-day bucket.
How is resample() different from groupby()?
- It only works on strings
- It cannot aggregate
- It sorts data only
- resample is groupby specialized for time intervals
Answer: resample is groupby specialized for time intervals. resample is essentially a time-aware groupby over date buckets.
Which frequency string means hourly?
- 'h'
- 'D'
- 'M'
- 'Y'
Answer: 'h'. 'h' (or 'H' in older pandas) buckets data by hour.