Sub topic 12
Aligning bins with origin and offset
This chapter assumes you can already call resample() with a fixed frequency such as "4h" or "2D" on a Series with a DatetimeIndex. It also assumes you know the closed and label options.
Why bin alignment matters
Bin edges are the timestamps where one bin ends and the next begins. For a fixed-length frequency, pandas has to pick where the first edge goes. By default it uses midnight of the first day in your data. That choice is often correct, but not always.
Suppose a 4-hour frequency should line up with shifts that start at 01:00, or your data starts at 09:30 and you want bins that start at 09:30. The origin and offset arguments control this.
The default: origin="start_day"
Edges are built from midnight of the first day, stepping forward in multiples of the frequency. The following code compares the default with an offset.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=10, freq="h")
s = pd.Series(range(10), index=idx)
print(s.resample("4h").sum())
print(s.resample("4h", offset="1h").sum())The default call gives bins starting at 00:00, 04:00 and 08:00, with sums 6, 22 and 17.
offset="1h" shifts every edge forward by one hour. The first bin now starts at 2023-12-31 21:00 and holds only the 00:00 row. The later bins start at 01:00, 05:00 and 09:00, with sums 10, 26 and 9. The first bin starts before your data because it has to contain the first timestamp.
Choosing an origin
origin sets the reference point that edges are counted from. It accepts these values:
| Value | Edges are counted from |
|---|---|
| `"start_day"` | Midnight of the first day in the data (default) |
| `"start"` | The first timestamp in the data |
| `"epoch"` | 1970-01-01 00:00:00 |
| `"end"` | The last timestamp in the data |
| `"end_day"` | Midnight after the last day in the data |
| A `Timestamp` | That exact moment |
Use "start" when the first observation should open the first bin.
import pandas as pd
idx = pd.date_range("2024-01-01 09:30", periods=8, freq="30min")
s = pd.Series(range(8), index=idx)
print(s.resample("2h").sum())
print(s.resample("2h", origin="start").sum())With the default origin, the bins start at 08:00, 10:00 and 12:00, and the sums are 0, 10 and 18. With origin="start", the bins start at 09:30 and 11:30, and the sums are 6 and 22. The second result is easier to read here, because each bin covers exactly four observations.
origin="epoch" is useful when several datasets must share the same bin edges regardless of when each one starts. Edges are then multiples of the frequency counted from 1970, so two series with different start times get matching bins.
How offset combines with origin
offset is a Timedelta or a string such as "30min". Pandas adds it to the edges after applying origin. A common use is a daily bin that runs from 09:00 to 09:00 instead of midnight to midnight:
daily_from_nine = s.resample("D", offset="9h").sum()Each bin is labeled with its 09:00 start.
Where these arguments do not apply
origin and offset affect only fixed-length frequencies, such as "h", "min" and "D". Calendar-based frequencies such as "MS" and "W" have their own anchors, so these arguments do not move their edges. To shift a weekly bin, use an anchored alias such as "W-MON" instead.
Exercise
Hourly readings run from 2024-03-01 00:00 to 2024-03-05 23:00. You want daily totals where each day runs from 06:00 to 06:00 the next morning. Which call does this?
Show answer
s.resample("D", offset="6h").sum(). The daily edges are moved six hours after midnight, so each bin is labeled with its 06:00 start. The first bin starts at 2024-02-29 06:00 because it must contain the 00:00 reading on March 1.