skip to content

In pandas, why does `resample('W')` stamp weekly traffic totals with Sundays while `resample('h')` stamps each hour by its start, and how do `closed` and `label` change that?

level: seniorimportance: should knowfreq 38%

answer

  1. defaults depend on the rule
  2. end-anchored rules close right
  3. W means weeks ending Sunday
  4. origin anchors fixed-length bins

basics

~20 s

closed and label default to 'left' except for end-anchored rules ME, QE, YE, BME, BQE, BYE and W, which default to 'right'. So 'W' (W-SUN) buckets Monday to Sunday and stamps the Sunday; pass closed and label to change it.

solid answer

~40 s

`closed` picks which bucket edge is included and `label` picks which edge stamps the row. Both default to `'left'`, except for the end-anchored rules `'ME'`, `'QE'`, `'YE'`, `'BME'`, `'BQE'`, `'BYE'` and `'W'`, which default to `'right'`. `'W'` is `'W-SUN'`, so a total stamped `2026-03-08` covers Monday 2 to Sunday 8 March. Joining that to a table keyed by week start shifts every week. For Monday-stamped weeks use `resample('W-MON', closed='left', label='left')`. For fixed rules like `'15min'` or `'7h'`, `origin` (default `'start_day'`) and `offset` set where the bin grid starts; with a calendar rule like `'W'` pandas warns that they do not take effect.

code

python · 13 lines
python
import pandas as pd

# Hourly rows from Monday 2 March to Sunday 15 March 2026 (two weeks)
idx = pd.date_range("2026-03-02", "2026-03-15 23:00", freq="h")
visits = pd.Series(1, index=idx)

print(visits.resample("W").sum())
# 2026-03-08    168   Mon 2 .. Sun 8 March, stamped with the Sunday
# 2026-03-15    168

print(visits.resample("W-MON", closed="left", label="left").sum())
# 2026-03-02    168   Mon 2 .. Sun 8 March, stamped with the Monday
# 2026-03-09    168

go deeper

for a junior

Recall that resample stamps hourly and daily buckets with their start, and that weekly and month-end rules stamp with the end.

for a middle

Explain closed versus label, the list of end-anchored rules that default to 'right', and that 'W' means weeks ending Sunday.

for a senior

Diagnose an off-by-one-week or off-by-one-hour join from the defaults, fix it with an explicit anchor such as 'W-MON' plus closed and label, and use origin for sub-daily bins that do not divide a day.

for a principal

Set a house convention for period stamps, start or end, so every pipeline resamples with explicit closed, label and anchor and reports from different teams line up.

## Two arguments, two jobs Every resample bucket is an interval between two edges. Two arguments decide how pandas uses them: - **`closed`** decides which edge belongs to the bucket. `closed='left'` means `[start, end)`: a row stamped exactly at `start` is in, one at `end` is not. `closed='right'` means `(start, end]`. - **`label`** decides which edge stamps the output row. `label='left'` writes `start`; `label='right'` writes `end`. They are independent, but they are usually set together, because a left-closed bucket labelled with its right edge (or the reverse) is hard to read. ## The defaults depend on the rule Both arguments default to `None`, and pandas resolves `None` from the rule: | rule | default `closed` | default `label` | a row stamped `2026-03-08` means | |---|---|---|---| | `'min'`, `'h'`, `'D'`, `'MS'`, `'QS'`, `'YS'` | `'left'` | `'left'` | the period that **starts** then | | `'W'` (`'W-SUN'`), `'W-MON'`, ... | `'right'` | `'right'` | the week that **ends** then | | `'ME'`, `'QE'`, `'YE'`, `'BME'`, `'BQE'`, `'BYE'` | `'right'` | `'right'` | the period that **ends** then | The end-anchored rules are named after the offset that sits on the end of a period, so stamping with the end edge is consistent with the name. For month, quarter and year ends pandas also stretches the right edge to the last moment of that day, so intraday rows on 31 January still land in January's bucket rather than February's. ## Why the weekly stamp surprises people `'W'` is shorthand for `'W-SUN'`, weeks anchored on Sunday. With the right-closed, right-labelled default, hourly traffic from Monday 2 March to Sunday 8 March 2026 becomes **one row stamped 2026-03-08**. Three common failures follow: 1. A dashboard or table keyed on **week start** (Monday 2 March) joins that row to the wrong week, shifting the whole series by one week. 2. A team mixing `resample('D')` (start-labelled) with `resample('W')` (end-labelled) in one chart sees daily points that seem to lead the weekly ones. 3. A business week that starts on Monday expects Monday's traffic in the week that starts that day, which the default does honour, but the label points at the following Sunday. The fix is to state the week you mean: - `resample('W-MON', closed='left', label='left')` buckets `[Monday, next Monday)` and stamps the Monday. - Or keep `'W'` and relabel deliberately, then document it. The same care applies to hourly data if someone passes `label='right'` for "hour ending" reporting: a value stamped `09:00` then describes 08:00 to 09:00, and comparing it to a start-labelled series is off by one bucket. ## `origin` and `offset`: where the grid starts `closed` and `label` say how a bucket uses its edges; `origin` says **where the edges are**. It applies to fixed-length rules such as `'h'`, `'min'` or `'s'`, and their multiples: - `'start_day'` (default): edges are counted from midnight of the first day in the data. - `'start'`: from the first timestamp itself. - `'epoch'`: from 1970-01-01, so any two runs over any date range share the same edges. - `'end'`, `'end_day'`: counted backward from the last timestamp or the midnight after the last day; these also switch the default `closed` and `label` to `'right'`. - a **timestamp**, for a custom anchor. `offset` adds a `Timedelta` to the origin, for example `resample('h', offset='30min')` for buckets from half past to half past. This matters when the bucket length does not divide a day. With `'7min'` or `'7h'` and the default `'start_day'`, a job processing yesterday and a job processing the last two days place their edges differently, so the "same" bucket totals differ. Setting `origin='epoch'` or a fixed timestamp makes the edges reproducible. With a calendar rule such as `'W'` or `'ME'`, pandas warns (`RuntimeWarning`) that `origin` and `offset` do not take effect; anchor those through the rule itself, as in `'W-MON'`. ## Checklist for a resample in shared code - Write `closed` and `label` explicitly on any weekly, monthly, quarterly or yearly rule. - Name the week anchor (`'W-MON'`, `'W-SUN'`) instead of bare `'W'`. - Use `origin='epoch'` or a fixed timestamp for sub-daily bins that do not divide 24 hours. - Test one boundary row, such as a reading stamped exactly on an edge, and assert which output row holds it.

  • Why can two jobs that both run `resample('7h').sum()` produce different buckets for the same hours?
    With the default `origin='start_day'`, edges are counted from midnight of the first day each job sees. Seven hours does not divide 24, so a job starting a day earlier lands its edges on different hours. Passing `origin='epoch'` or a fixed timestamp anchors the grid so both jobs share edges.
  • On `resample('ME')`, does a reading at 31 January 18:00 land in January or February?
    January. `'ME'` is right-closed and right-labelled, and for month-end rules pandas moves the right edge to the last moment of the labelled day, so every row on 31 January belongs to the bucket stamped `2026-01-31`. Only rows from 1 February onward fall into February's bucket.

  • resample('W') stamps each week with its Monday
  • closed and label default to 'left' for every rule
  • label decides which rows fall into a bucket
  • origin='epoch' aligns weekly and monthly buckets too
  • A month-end bucket drops intraday rows on the last day