Sub topic 8
Which side of the bin is closed, and how bins are labeled
This chapter assumes you can build a DatetimeIndex and call resample() with a frequency string, as covered earlier. Here you will learn how pandas decides which rows fall into a bin, and which timestamp it uses to name that bin.
Two separate choices
Every bin has a left edge and a right edge. Two parameters control how the bin works:
closedsays which edge is inclusive."left"gives[start, end)."right"gives(start, end].labelsays which edge becomes the bin's timestamp in the result index:"left"or"right".
The two settings are independent. closed changes which rows are counted. label changes only the index of the result. Mixing them up is the most common source of "off by one bin" confusion.
Defaults depend on the frequency
For most frequencies, such as "D", "h", "2D", and "MS", the defaults are closed="left" and label="left". For frequencies that anchor to the end of a period, the defaults are "right" for both. These include "W", "ME", "QE", and "YE".
That is why the weekly example earlier labeled each bin with its Sunday. A Sunday row belongs to the week that ends on that Sunday, and the label is that same Sunday.
Older pandas versions spell the month-end alias "M". Pandas 2.2 and later prefer "ME".
Comparing the settings
Run both versions on ten daily values and compare:
import pandas as pd
idx = pd.date_range("2024-01-01", periods=10, freq="D")
s = pd.Series(range(10), index=idx)
print(s.resample("2D").sum())
print(s.resample("2D", closed="right", label="right").sum())The first call uses the defaults. Its bins are [01-01, 01-03), [01-03, 01-05), and so on, so the sums are 1, 5, 9, 13, and 17. The labels are 2024-01-01, 01-03, 01-05, 01-07, and 01-09.
The second call uses (start, end] bins. The row for 2024-01-01 now sits alone in the bin ending on that day, because that day is its right edge. The sums are 0, 3, 7, 11, 15, and 9, labeled 2024-01-01 through 2024-01-11. Both runs total 45, so no data is lost. The rows are only grouped differently.
Choosing a setting
| Goal | Typical choice |
|---|---|
| Label a period by when it starts | `closed="left"`, `label="left"` |
| Label a period by when it ends, such as an end-of-week report | `closed="right"`, `label="right"` |
| Keep the standard grouping but name bins by their end | `label="right"` only |
Changing only label is safe. It shifts the index and leaves every value alone. Changing closed can change the values, and it matters most when your timestamps fall exactly on edges, such as midnight readings.
Common errors
- Assuming
closedandlabelmove together. Settinglabel="right"does not move any rows between bins. - Comparing results across frequencies without checking defaults.
"W"and"7D"produce different bins, because"W"is right-closed and anchored to Sunday, while"7D"is left-closed and starts at the first day of your data. - Joining a resampled result to another series without checking whether the labels mark starts or ends.
Exercise
A series has one reading per day. You call resample("W").sum(). A reading is stamped on a Sunday. Which weekly bin does it join, and what label does that bin get?
Show answer
With "W", closed and label both default to "right". The Sunday reading joins the bin that ends on that Sunday, since the bin is (previous Sunday, this Sunday]. The bin is labeled with that same Sunday.