Sub topic 12
Aligning bins with origin and offset
This chapter assumes you know how resample() builds bins (covered earlier in this tutorial) and how closed and label pick the bin side and name. Here you control where the bin grid itself starts. The origin and offset arguments decide that.
Why bin alignment matters
When you resample to "D", "h", or "15min", pandas needs a reference point. It lays down edges at multiples of the frequency starting from that point. If the frequency divides evenly into a day, such as "h", "15min", or "6h", the reference point rarely matters. The edges land on round clock times either way.
The reference point matters in two cases:
- The frequency does not divide 24 hours evenly, for example
"7h"or"5min"on data with odd start times. - You want bins that start at a non-midnight time, such as a trading day that begins at 09:00.
Both arguments apply to fixed-length frequencies such as "h", "min", "s", "D", and multiples of them ("7h", "2D"). Calendar-anchored frequencies such as "W" and "MS" always snap to the calendar. origin and offset do not move their edges.
The origin argument
origin names the timestamp that the grid is built from. Edges fall at origin + k * freq for whole numbers k, both before and after the origin.
| Value | Meaning |
|---|---|
| `"start_day"` (default) | Midnight of the first day in the data |
| `"start"` | The first timestamp in the data |
| `"epoch"` | `1970-01-01 00:00:00` |
| `"end"` | The last timestamp in the data (pandas 1.3 and later) |
| `"end_day"` | Midnight at the end of the last day (pandas 1.3 and later) |
| a `Timestamp` | Any moment you choose |
Default start_day versus start
The data below starts at 10:00 and runs for 12 hours. A 7-hour frequency does not divide a day, so the choice of origin changes the bins.
import pandas as pd
idx = pd.date_range("2024-01-01 10:00", periods=12, freq="h")
s = pd.Series(range(12), index=idx)
print(s.resample("7h").sum())
print(s.resample("7h", origin="start").sum())If you use pandas older than 2.2, write "H" instead of "h".
The default grid starts at midnight, so its edges are 00:00, 07:00, 14:00, and 21:00. With origin="start", the edges start at 10:00.
| Origin | Bin label | Rows included | Sum |
|---|---|---|---|
| `start_day` | 2024-01-01 07:00 | 10:00 to 13:00 | 6 |
| `start_day` | 2024-01-01 14:00 | 14:00 to 20:00 | 49 |
| `start_day` | 2024-01-01 21:00 | 21:00 | 11 |
| `start` | 2024-01-01 10:00 | 10:00 to 16:00 | 21 |
| `start` | 2024-01-01 17:00 | 17:00 to 21:00 | 45 |
Both runs add up to 66, which is the sum of 0 through 11. Only the grouping differs.
With start_day, the first bin is labeled 07:00, which is earlier than any row. It is a partial bin that holds only the rows from 10:00 to 13:00. This is normal. The grid is anchored to midnight, not to your data.
Why epoch gives reproducible grids
With start_day and start, the grid depends on your data. If you slice a series to a different first day, a "7h" grid can shift. Two datasets resampled separately will not line up.
origin="epoch" ties the grid to a fixed point in time. The same frequency gives the same edges for any data. The effect is clearest with multi-day frequencies.
import pandas as pd
idx = pd.date_range("2024-01-01", periods=6, freq="D")
s = pd.Series(range(6), index=idx)
print(s.resample("2D").sum())
print(s.resample("2D", origin="epoch").sum())The default grid starts on the first day, 2024-01-01. The epoch grid starts at 1970-01-01 and steps two days at a time. 2024-01-01 is day 19723 after the epoch, which is an odd number. So the epoch bins start on 2023-12-31, 2024-01-02, and so on.
| Origin | Bin labels | Sums |
|---|---|---|
| `start_day` | 01-01, 01-03, 01-05 | 1, 5, 9 |
| `epoch` | 2023-12-31, 01-02, 01-04, 01-06 | 0, 3, 7, 5 |
The first epoch bin holds only 2024-01-01, because 2023-12-31 has no data.
Choosing an origin
flowchart TDReplace that sketch with the real diagram below.
The offset argument
offset is a Timedelta or a string such as "9h" that is added to the origin. It shifts every bin edge by the same amount. Use it to start days, hours, or other bins at a time that is not on the grid.
A common case is a business day that begins at 09:00:
import pandas as pd
idx = pd.date_range("2024-01-01", periods=48, freq="h")
s = pd.Series(1, index=idx)
print(s.resample("D", offset="9h").sum())The grid edges are now 09:00 each day. The data begins at midnight, which is before the first 09:00 edge, so pandas adds an earlier bin that starts on 2023-12-31 at 09:00.
| Bin label | Rows included | Count |
|---|---|---|
| 2023-12-31 09:00 | 2024-01-01 00:00 to 08:00 | 9 |
| 2024-01-01 09:00 | 2024-01-01 09:00 to 2024-01-02 08:00 | 24 |
| 2024-01-02 09:00 | 2024-01-02 09:00 to 23:00 | 15 |
The first and last bins are partial. If you want only complete days, drop them after resampling, or slice the input so it starts at a bin edge.
A negative offset shifts the other way. offset="-15h" produces the same grid as offset="9h" for a 24-hour frequency, because the edges differ by exactly one period. Prefer the positive form, since it reads as "the day starts at 09:00".
Combining origin and offset
The two arguments add together. The effective anchor is origin + offset. This is useful with "epoch":
import pandas as pd
idx = pd.date_range("2024-01-01", periods=48, freq="h")
s = pd.Series(1, index=idx)
result = s.resample("12h", origin="epoch", offset="3h").sum()
print(result)The edges are at 03:00 and 15:00 each day, regardless of where the data starts. Because they come from the epoch, they are the same for any slice of this series.
A fixed Timestamp as origin
You can give origin an exact Timestamp. The grid extends in both directions from it, so the origin does not have to be inside your data:
import pandas as pd
idx = pd.date_range("2024-01-01 10:00", periods=12, freq="h")
s = pd.Series(range(12), index=idx)
anchor = pd.Timestamp("2024-01-01 02:00")
print(s.resample("7h", origin=anchor).sum())The edges are 02:00, 09:00, 16:00, and 23:00. The bin starting at 09:00 holds the rows from 10:00 to 15:00, which sum to 15. The bin starting at 16:00 holds 16:00 to 21:00 and sums to 51.
If the data is time zone aware, give the Timestamp the same time zone. Time zone handling is covered in its own chapter.
Common errors
- Using
base. Older code usesresample("7h", base=3). Thebaseargument was removed in pandas 2.0. Useoffset="3h"for the same shift. The oldloffsetargument is also gone. Shift the result index yourself withresult.index += pd.Timedelta(...). - Expecting
originto move month or week bins.resample("MS", origin="start")still gives bins at the start of each month. To get months that begin on the 15th, shift the index with aTimedelta, resample, and shift the labels back. - Reading the first label as the first data point. With
start_dayorepoch, the first label can be earlier than any row. Check the first bin's row count before using it. - Comparing grids from different slices. If you build features from two slices of one series with a frequency such as
"7h", useorigin="epoch"or a fixedTimestamp. Otherwise each slice gets its own grid.
Exercises
Exercise
You have hourly readings starting at 2024-03-01 00:00 for 48 hours. You want daily sums where each "day" runs from 06:00 to 05:59. Which argument do you use, and how many bins do you get?
Show answer
Use offset="6h": s.resample("D", offset="6h").sum(). You get three bins labeled 2024-02-29 06:00, 2024-03-01 06:00, and 2024-03-02 06:00. They hold 6, 24, and 18 rows. The first and last bins are partial.
Exercise
A colleague resamples two slices of the same series with "7h" and gets bin edges that do not match. What causes this, and what is the fix?
Show answer
The default origin is start_day, which anchors the grid to midnight of each slice's first day. If the slices start on different days, a 7-hour grid no longer repeats at the same times. Use origin="epoch" or a fixed Timestamp so both slices share one grid.
Exercise
Why does s.resample("2D").sum() on data starting 2024-01-01 produce a different first label than s.resample("2D", origin="epoch").sum()?
Show answer
The default grid starts at midnight of the first data day, so the first label is 2024-01-01. The epoch grid steps in two-day units from 1970-01-01. 2024-01-01 is an odd number of days after the epoch, so its bin starts one day earlier, on 2023-12-31.