pandas · Chapter 19 of 22

Resampling and rolling windows

By the end of this chapter you can total a table by day, week or month with resample, see and fill the days that have nothing in them, spread a monthly number over its days, and smooth a noisy daily series with a rolling window that covers the days you mean. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 4 tasks
  • pandas
  • In the all labs plan

The first 2 chapters of pandas are free. This one comes with the all labs plan.

Why you need this

Shabnam wants two pictures of the year: the website's revenue for every day and for every month, and a smooth trend line through it, because a single day jumps around too much to read (Sundays are busy, Diwali was wild). Harpreet wants each day's sales held against her monthly target, so a slow month shows up in its first week, not after it ends.

Chapter 18 gave you dates, so the first try is a groupby on the date:

import pandas as pd

lines = pd.read_csv("/workspace/data/orders_site.csv")
lines["placed_at"] = pd.to_datetime(lines["placed_at"])
lines["line_rs"] = lines["price_rs"] * lines["qty"]
by_date = lines.groupby(lines["placed_at"].dt.date)["line_rs"].sum()
print(len(by_date))
print(by_date.loc[pd.Timestamp("2026-06-12").date():pd.Timestamp("2026-06-17").date()])
363
placed_at
2026-06-12    248632.0
2026-06-13    267530.0
2026-06-16    254030.0
2026-06-17    141337.0
Name: line_rs, dtype: float64

The year has 365 days, and the groupby gives 363. The website was down on 14 and 15 June, so those days had no lines, and a groupby makes a group only for a key it sees: the two days are not 0, they are not there. Every report built on it goes quietly wrong. A chart draws a straight line over the outage. An average of "the last 7 rows" on 17 June covers 9 days. Counting months with groupby works, but each month is a label you build by hand, and nothing marks a month that had no sales at all.

pandas has tools made for time: resample, a groupby whose groups are days, weeks or months of the calendar, every one of them, and rolling, a window that slides along a series.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    revenue.py: the website's revenue by day and by month

    Write two functions in revenue.py, each given lines: the website's order lines from orders_site.csv without the duplicates, the test orders and the cancelled lines, with placed_at already dates (datetime64) and a column line_rs, each line's price_rs times qty (a missing value where qty is blank). The table's row labels are 0, 1, 2 and so on; it must not be changed.

    Use resample for both. daily_revenue(lines) returns a Series of the sum of line_rs for every day, labelled by the day (a date at midnight), from the first line's day to the last line's day in date order. A day with no lines is still there, with 0: the website had no orders on 14 and 15 June 2026, and those two days must be in the result.

    monthly_revenue(lines) returns a Series of the sum of line_rs for every calendar month, labelled by the month's first day (2025-10-01 for October 2025), in date order.

    The Check tries the real lines, and a small table of 5 lines from 26 February to 2 March 2026 with no lines on 27 February and 1 March, and one line whose line_rs is missing.

  2. Task 2

    trend.py: a 7-day average that does not skip the outage

    Write week_average(daily) in trend.py. daily is a Series of the website's revenue on each day that had orders, labelled by the day (dates at midnight, in date order), as a groupby on the date gives it: a day with no orders is not in it at all, so 14 and 15 June 2026 are missing.

    Return a Series labelled by every calendar day from daily's first day to its last, in date order, whose value for a day is a rolling average: the average revenue of that day and the six days before it, where a day missing from daily counts as 0. The first six days have no full week behind them: their values are missing (NaN).

    The Check tries the real daily revenue and 10 days in January 2026 with 4 and 9 January missing.

  3. Task 3

    targets.py: Harpreet's targets, day by day

    Write target_pace(daily, targets) in targets.py. daily is a Series of revenue for every calendar day, labelled by the day (dates at midnight, in date order, no day missing). targets is a Series of Harpreet's monthly targets in rupees, labelled by each month's first day (2025-10-01 for October), in date order.

    Return a DataFrame labelled by daily's days, in the same order, with exactly three columns in this order: revenue (daily's values), target (the target of the day's month spread evenly over its days: the month's target divided by the number of days in that month) and gap_rs (revenue minus target). All three are decimals.

    The Check tries the sarees' daily website revenue for the year with the saree targets from targets_wide.csv, and 28 January to 6 February 2026 twice: with targets for January and February only, and with targets for December 2025 to March 2026 (the extra months are not in the result).

  4. Task 4 · interview question

    Interview: weeks.py, a first week that looks too small

    Shabnam's weekly report comes from full_weeks(daily) in weeks.py, and its first week, labelled 5 October 2025, is less than half of the next one, with nothing wrong in the data. daily is a Series of the website's revenue for every calendar day, labelled by the day (dates at midnight, in date order, no day missing).

    Fix full_weeks so it returns the weekly totals, labelled by the Sunday that ends each week (Monday to Sunday), in date order, keeping only the weeks that have all seven of their days in daily. A week that daily starts or ends in the middle of is left out.

    The Check tries the real daily revenue, Thursday 5 March to Tuesday 24 March 2026 (two full weeks), Monday 2 March to Sunday 15 March 2026 (every week full) and Wednesday 4 March to Sunday 8 March 2026 (no full week: an empty Series).

    Devika will ask: why did the week labelled 5 October 2025 have only 5 days in it, and how do you check the ends of a resample?

What you will learn

  • resample: a groupby for time on a date index, one group per day, week or month ("D", "W", "ME", "MS").
  • The ends: which dates a bin covers and how it is labelled.
  • Empty days: 0 for a sum, NaN for a mean, and asfreq to show the gaps in a daily series.
  • Upsampling: a weekly or monthly number onto every day, with ffill and reindex(method="ffill"), and days_in_month to spread it.
  • rolling: a moving window (rolling(7).mean(), min_periods, center), and a window of 7 rows against one of 7 days.
  • expanding and ewm, in a paragraph.

More in Time

Build "Resampling and rolling windows" yourself

Kargha, a Varanasi handloom brand, sells through a website, a marketplace and a store. You make one true picture of its sales. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of pandas are free. This one comes with the all labs plan.