All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 7 · about 20 minutes · uses Lesson 7.1. You’ll be able to turn a daily series into weekly, monthly or quarterly ones.

resample is groupby for time: it puts each row into a bucket (week, month, quarter) by its date and summarises each bucket.

In everyday terms: sorting a year of receipts into twelve monthly envelopes, then writing the total on each envelope.

1. resample

Pythonorders.csv

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

Pythonorders.csv

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:

Pythonorders.csv
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:

Daily series (index = date).
daterevenue
03-3050
03-3170
04-0140
04-0260
Buckets. daily.resample('MS'): 30-31 March → March; 1-2 April → April.
Sum each.
monthrevenue
2026-03-01120
2026-04-01100
Careful. April has 2 days so far; its 100 is not comparable to a full month.
Per store. df.groupby('store').resample('MS', on='date')['revenue'].sum()

4. Your turn

Your turnorders.csv
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

  1. Average daily revenue per month (not total).
  2. Weekly units per category, as a category × week grid.
  3. Why flag partial periods?
Answers
  1. daily.resample('MS').mean() on a daily series with every day present.
  2. full.groupby(['category', pd.Grouper(key='date', freq='W-SUN')])['qty'].sum().unstack().
  3. 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

1What does daily.resample('MS').sum() give?
2Which rule makes weeks that run Monday to Sunday?
3How do you resample each store separately?

Sources

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.