Module 7: Time series · Lesson 7.2 · about 20 min
Daily to weekly and monthly with resample
Change the frequency of a time series with resample: daily to weekly, monthly or quarterly totals and averages, week start days, per-store resampling with groupby, and Grouper for dates inside a normal table.
Module 7 · about 20 minutes · uses Lesson 7.1. You’ll be able to turn a daily series into weekly, monthly or quarterly ones.
In everyday terms: sorting a year of receipts into twelve monthly envelopes, then writing the total on each envelope.
1. resample
Common rules: 'D' day, 'W-SUN' week ending Sunday, 'MS' month (labelled by its start), 'QS' quarter, 'YS' year. Any aggregation works: sum, mean, max, count, or agg([...]).
2. Directly from a table, and per store
pd.Grouper(key='date', freq='QS') is a date bucket you can use inside an ordinary groupby next to other keys.
3. Partial periods
The first and last buckets may be incomplete (the data starts mid-week, or the current month isn’t over). Comparing a partial month with full ones is a classic dashboard mistake. Check the number of days in each bucket:
- resample('W')
- Weekly buckets (weeks ending Sunday by default).
- resample('MS') / ('ME')
- Monthly buckets labelled by the month start or month end.
- .sum() / .mean()
- Sum for totals (revenue); mean for levels (price, temperature).
- Partial periods
- A month with only 10 days of data looks like a crash: drop or flag it.
- Physical meaning
- Each bucket total is the sum of the days inside it, so the monthly totals add back to the daily total. A sum depends on how many days a bucket has (28 or 31), so compare months by daily average when lengths differ.
“The last month fell sharply.” Often it is not over yet: resample builds the current month from the days so far. Compare full periods, or compare the same number of days.
Watch it happen, one step at a time:
| date | revenue |
|---|---|
| 03-30 | 50 |
| 03-31 | 70 |
| 04-01 | 40 |
| 04-02 | 60 |
daily.resample('MS'): 30-31 March → March; 1-2 April → April.| month | revenue |
|---|---|
| 2026-03-01 | 120 |
| 2026-04-01 | 100 |
df.groupby('store').resample('MS', on='date')['revenue'].sum()4. Your turn
How it works
Principle: resample is a groupby where the key is a time bucket computed from each date. Downsampling (day → month) aggregates; upsampling (month → day) creates empty rows you then fill (asfreq, ffill, interpolate).
Labels and edges: each bucket has a label (start or end) and a closed side. Defaults differ between rules; when it matters (fiscal weeks, reporting cut-offs), set label= and closed= explicitly and check one bucket by hand.
5. Practice
- Average daily revenue per month (not total).
- Weekly units per category, as a category × week grid.
- Why flag partial periods?
Answers
daily.resample('MS').mean()on a daily series with every day present.full.groupby(['category', pd.Grouper(key='date', freq='W-SUN')])['qty'].sum().unstack().- An incomplete week or month looks like a drop; compare like with like, or show it clearly as partial.
New words: resample, frequency rule, Grouper, downsample, upsample, partial period.
- resample(rule) groups rows into time buckets by the date index.
- ‘W’, ‘MS’, ‘QS’: weekly, monthly, quarterly.
- sum for flows (revenue), mean for levels (price).
- Flag or drop the partial first and last periods.
Check yourself
Sources
- pandas documentation, User Guide: Time series / date functionality: resampling https://pandas.pydata.org/docs/user_guide/timeseries.html#resampling
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.