Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 64 ยท Time Series

Resampling and Time Windows

Learn pandas resample() to turn daily data into weekly or monthly totals, fill gaps when upsampling, group with Grouper and handle business days.

Raw time series rarely arrive in the shape you want to report. A shop logs every order, a sensor writes a reading every few seconds, a bank posts transactions all day, yet the question is usually "how much per week?" or "what was the monthly average?". Resampling changes the frequency of a time series: it cuts the timeline into regular windows (called bins) and summarizes everything that falls into each one.

In this lesson you will learn how resample() works, how to choose the right frequency string, what happens when you go the other way (from monthly to daily), and how to deal with gaps. You should already be comfortable with a DatetimeIndex; if not, read Time Series Basics first. Resampling is groupby for time, so groupby Basics will feel familiar.

Downsampling: fewer, bigger windows

df.resample(rule) needs a DatetimeIndex (or a on="column" argument). It returns a resampler, which does nothing until you call an aggregation such as sum(), mean() or agg().

flowchart LR
    A["10 daily rows"] --> B["resample('W')"]
    B --> C["bin ending Sun Jan 4<br/>4 rows"]
    B --> D["bin ending Sun Jan 11<br/>6 rows"]
    C --> E["sum, mean, ohlc ..."]
    D --> E
    E --> F["2 rows, one per week"]

Downsampling: every row is assigned to a window, then each window is reduced to one row.

Python Daily to weekly totals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weekly bins end on Sunday and are labelled with that Sunday ("W" is short for "W-SUN"). January 1, 2026 is a Thursday, so the first bin holds only four days (Thursday to Sunday) and sums to 22 units. A multiple such as "4D" simply starts counting at the first timestamp in your data.

Frequency strings you will use

Code Meaning Label of each bin
"h", "min", "s" hour, minute, second start of the interval
"D" calendar day the day
"B" business day (Mon-Fri) the day
"W" / "W-MON" week ending on Sunday / Monday the closing day
"MS" / "ME" month start / month end first / last day of the month
"QE", "YE" quarter end, year end last day of the period

In pandas 3 the old aliases "M", "Q" and "Y" are gone; use "ME", "QE" and "YE" (or the S forms for start-anchored bins). Lowercase "h" is hours; the old uppercase "H" no longer works.

Open-high-low-close with ohlc()

Finance and sensor work often needs the first, highest, lowest and last value of each window. ohlc() gives all four in one call:

Python ohlc per window

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first window covers January 1-5, so open is 50 (the first value), high 80, low 20 and close 20 (the last value in the window).

Which edge does a bin belong to?

Every bin has two edges, and you must decide which one is included and which one names the bin. Two arguments control this: closed ("left" or "right") and label ("left" or "right"). The defaults were chosen so that common cases do the intuitive thing: "ME", "QE", "YE" and "W" are right-closed and right-labelled, everything else is left-closed and left-labelled.

Python Week labels and monthly bins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Both monthly lines hold the same number (91) but carry different labels. Choose "MS" when you want to join the result with other tables keyed by the first day of the month, and "ME" when the month end is more natural, for example for balance sheets.

Plain resample("W") labels each week by its Sunday. That surprises people who think of weeks as starting on Monday. If your week starts on Monday, use "W-SUN" (the week ends on Sunday) with the default settings: Monday to Sunday is one bin labelled by the Sunday.

Upsampling: more rows than you started with

Going from a coarse to a fine frequency creates timestamps that had no data. resample() inserts them as NaN, and you decide how to fill them. The monthly targets below only exist for month ends:

Python Monthly to daily

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three tools do the filling:

  • asfreq() only inserts the missing timestamps and leaves NaN; an integer series becomes float because NaN is a float.
  • ffill() / bfill() copy the previous or next known value. Use them for things that hold until changed, such as a price list or a target.
  • interpolate() estimates in-between values. Use it for smooth quantities such as temperature, not for counts.

Series.asfreq(freq) is also available directly on the series without creating a resampler. It does no aggregation: it picks the values that sit exactly on the new grid and discards the rest. asfreq("D", method="ffill") is the shortcut for "upsample and forward-fill".

Resampling with Grouper and groupby

Often the time column is not the index, or you need a time window and another key such as the shop or customer. pd.Grouper(key=..., freq=...) brings the time bins into groupby:

Python Grouper and gaps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the daily result: March 3, 6 and 7 had no orders at all, yet they appear with a sum of 0. That is exactly the behaviour you want for charts and rolling averages, because a missing day silently stretches or distorts the next calculation. Remember that sum() of an empty bin is 0, while mean() of an empty bin is NaN.

Business days, month ends and offsets

Calendars are not uniform, so pandas has offsets, objects that know how to jump to the next business day, month end or quarter start. You use them with + on timestamps and as freq values:

Python Business days and offsets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

BDay(3) from Thursday March 5 lands on Tuesday March 10 because it skips the weekend. Business-day logic only knows about Saturday and Sunday; for public holidays you can build a custom calendar with pd.offsets.CustomBusinessDay.

Sum of nothing is zero, mean of nothing is NaN. After resampling a sparse series, check which one you want. A day with no sales has revenue 0, but a day with no sensor reading has an unknown temperature, and turning it into 0 would be a lie. Use sum(min_count=1) to get NaN for empty bins instead of 0.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does df.resample('W').sum() return when df has one row per day?

    resample('W') bins end on Sunday and are labelled with that Sunday. Daily rows are reduced to one row per week by the aggregation.
  2. 2. Which frequency string gives one bin per calendar month, labelled with the first day of the month?

    "MS" is month start. "ME" is month end, and the old bare "M" alias was removed in pandas 3.
  3. 3. You upsample a monthly price series to daily and want each day to keep the last known price. Which call do you use?

    ffill() copies the last known value forward. asfreq() would leave NaN, interpolate() would draw a line between prices, and sum() makes no sense for a price.
  4. 4. A day without any sensor readings appears after resample('D').mean(). What is its value?

    The mean of an empty bin is NaN, because there is nothing to average. Only sum() returns 0 for an empty bin (unless you pass min_count).

Summary

  • resample(rule) groups a time-indexed object into regular windows, then you aggregate with sum(), mean(), agg(), ohlc() and so on.
  • Use pandas 3 aliases: "D", "B", "W", "MS", "ME", "QE", "YE", "h", "min".
  • closed and label decide which edge belongs to a bin and what name it gets; weekly and month-end bins are labelled by their closing day.
  • Upsampling creates empty rows: fill them with asfreq(), ffill()/bfill() or interpolate().
  • pd.Grouper(key="col", freq=...) combines time bins with other groupby keys when the date is a column.
  • Resampling puts empty periods back into the data, but remember sum() gives 0 and mean() gives NaN for them.

Next: Rolling, Expanding and EWM shows how to slide a window across a series instead of cutting it into fixed bins.