Sub topic 8
Which side of the bin is closed, and how bins are labeled
This chapter assumes you can build a DatetimeIndex, know what resample() returns, and have used .sum() or .mean() on a resampler. Here you will learn the two settings that decide which rows land in each bin (closed) and what timestamp each result row carries (label). Mixing these up is a common cause of off-by-one-period errors in resampled data.
Two separate decisions
Every bin has a left edge and a right edge. Two questions follow from that:
- Membership. If a timestamp sits exactly on an edge, which bin does it belong to? The
closedargument answers this. Withclosed="left"the bin is[left, right). Withclosed="right"the bin is(left, right]. - Labeling. Which edge is used as the index value of the output row? The
labelargument answers this, with"left"or"right".
The two settings are independent. label never moves a row from one bin to another. It only changes the name printed next to the aggregated value.
Edge cases only matter when timestamps fall exactly on edges. Daily data at midnight always does. Irregular timestamps such as 10:17:42 rarely do.
The defaults
pandas picks the defaults from the frequency you request.
| Frequency | Examples | `closed` | `label` |
|---|---|---|---|
| Fixed or start-anchored | `"h"`, `"D"`, `"2D"`, `"MS"`, `"YS"` | left | left |
| End-anchored | `"W"`, `"ME"`, `"QE"`, `"YE"` | right | right |
In pandas versions before 2.2, the month-end alias is "M" instead of "ME". The end-anchored defaults are the same.
The logic is that a monthly or weekly bin is usually named for the day it ends, such as "week ending Sunday" or "month ending January 31". Short intraday bins are usually named for the moment they start.
Weekly bins: the default in action
The weekly example from earlier in the tutorial uses "W", which is shorthand for "W-SUN".
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("W").sum())2024-01-07 21
2024-01-14 24
Freq: W-SUN, dtype: int64January 1, 2024 is a Monday. The first bin is (2023-12-31, 2024-01-07], which holds the values 0 through 6, and it is labeled with its right edge. The second bin holds 7, 8 and 9 and is labeled 2024-01-14, even though the data ends on January 10. The label is a bin edge, not the last timestamp present in the data.
Fixed-width bins with explicit closed and label
Now compare the default and the flipped settings on 2-day bins, using the same ten values.
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())2024-01-01 1
2024-01-03 5
2024-01-05 9
2024-01-07 13
2024-01-09 17
Freq: 2D, dtype: int64
2024-01-01 0
2024-01-03 3
2024-01-05 7
2024-01-07 11
2024-01-09 15
2024-01-11 9
Freq: 2D, dtype: int64In the default result, each bin is [start, start + 2 days). The bin labeled 2024-01-01 contains January 1 and 2, so its sum is 0 + 1 = 1.
In the second result, each bin is (end - 2 days, end]. The edges are the same dates, but the midnight timestamps now fall into the earlier bin:
2024-01-01is the right edge of a bin that starts on December 30. That bin contains only January 1, so its sum is 0.2024-01-03covers January 2 and 3, so its sum is 1 + 2 = 3.- The last bin, labeled
2024-01-11, holds only January 10, so its sum is 9.
The right-closed result has six rows where the default has five. Closing the right side moves the boundary rows, which can create a partial bin at the start or the end. Both results sum to 45, so no data is lost. Rows only move between bins.
Changing only the label
You can set closed and label independently. The common case is keeping the default membership but labeling each bin by its end time, so the label reads "up to this moment".
import pandas as pd
idx = pd.date_range("2024-01-01 00:00", periods=6, freq="h")
s = pd.Series(range(6), index=idx)
print(s.resample("3h").sum())
print(s.resample("3h", label="right").sum())
print(s.resample("3h", closed="right", label="right").sum())2024-01-01 00:00:00 3
2024-01-01 03:00:00 12
Freq: 3h, dtype: int64
2024-01-01 03:00:00 3
2024-01-01 06:00:00 12
Freq: 3h, dtype: int64
2024-01-01 00:00:00 0
2024-01-01 03:00:00 6
2024-01-01 06:00:00 9
Freq: 3h, dtype: int64The first two results have identical values, 3 and 12. Only the index differs. The third changes the grouping itself: the 00:00 reading is pushed into an earlier bin, and 03:00 joins the first bin.
| `closed` | `label` | Bins | Index shown |
|---|---|---|---|
| left | left | `[00:00, 03:00)`, `[03:00, 06:00)` | 00:00, 03:00 |
| left | right | same bins | 03:00, 06:00 |
| right | right | `(21:00, 00:00]`, `(00:00, 03:00]`, `(03:00, 06:00]` | 00:00, 03:00, 06:00 |
| right | left | same as above | 21:00, 00:00, 03:00 |
The combination closed="left", label="right" is a good choice when you want the label to show when the period finished without changing which readings count in it.
Month ends and times of day
Month-end aliases add one wrinkle. A reading at 2024-01-31 15:00 is still January, but a plain right-closed bin ending at midnight on January 31 would exclude it. To avoid that, pandas moves the right edge of each end-anchored calendar bin to the end of the day. Every timestamp in the final day therefore lands in that month, whatever its time of day.
import pandas as pd
idx = pd.date_range("2024-01-30", periods=4, freq="D")
s = pd.Series([1, 2, 3, 4], index=idx)
print(s.resample("ME").sum())
print(s.resample("MS").sum())2024-01-31 3
2024-02-29 7
Freq: ME, dtype: int64
2024-01-01 3
2024-02-01 7
Freq: MS, dtype: int64Both calls group the rows the same way: January 30 and 31 in one bin, February 1 and 2 in the other. Only the labels differ: month end versus month start. Choose the alias that gives the labels you want to join against or plot.
Gotchas
- Do not expect
labelto change totals. If two results have different values, the cause isclosedor the bin alignment, notlabel. - Check the first and last rows. A flipped
closedoften adds a partial bin at one end. Look at.count()per bin if you suspect a short one. - Labels are edges, not data timestamps. The last label can be later than your last observation, as in the weekly example above.
- Match
closedandlabelto your downstream convention. For example, "hourly volume ending at 10:00" suggestsclosed="right", label="right". Joins on labels fail silently if one side uses start labels and the other uses end labels.
Exercises
Exercise
Hourly data runs from 2024-01-01 00:00 through 05:00 with values 0, 1, 2, 3, 4, 5. What are the labels and sums of s.resample("3h", closed="right", label="left").sum()?
Show answer
The bins are (21:00, 00:00], (00:00, 03:00] and (03:00, 06:00], holding 0; 1+2+3; and 4+5. The sums are 0, 6 and 9. With label="left" the labels are the left edges: 2023-12-31 21:00 gets 0, 2024-01-01 00:00 gets 6, and 2024-01-01 03:00 gets 9.
Exercise
A colleague says that adding label="right" to s.resample("2D").sum() will change which days are summed together. Is that correct? What should they change instead?
Show answer
No. label only renames each bin with its right edge, so the sums stay the same. To change which rows fall in each bin, they should change closed (or the bin alignment with origin and offset).