Sub topic 14
Time zones and daylight saving time
This chapter assumes you know how to build a DatetimeIndex, how resample() bins rows, and how to downsample with sum(). It covers what changes when the index carries a time zone.
Why time zones matter for bins
A naive index has no zone, so every day is exactly 24 hours. A tz-aware index uses local clock time for calendar bins such as "D", "W" and "MS". Around a daylight saving time (DST) change, a local day can have 23 or 25 hours. resample() follows the local calendar, so the bins stay aligned with local midnight.
import pandas as pd
idx = pd.date_range(
"2024-03-09", "2024-03-11 23:00", freq="h", tz="America/New_York"
)
s = pd.Series(1, index=idx)
print(s.resample("D").sum())Each row counts as 1, so the sums are row counts per bin:
2024-03-09 00:00:00-05:00 24
2024-03-10 00:00:00-05:00 23
2024-03-11 00:00:00-04:00 24
Freq: D, dtype: int64On 2024-03-10 the clocks jump from 02:00 to 03:00, so that day has only 23 hourly rows. The bin labels also change their UTC offset, from -05:00 to -04:00.
Calendar days versus fixed durations
"D" means a calendar day on a tz-aware index. "24h" means exactly 24 hours of elapsed time. The two agree until the first DST change. After that, "24h" bins start at 01:00 local time instead of midnight. Use "D" when you want local days. Use "24h" only when you really need equal-length elapsed intervals.
| Frequency | Meaning on a tz-aware index | Length on a DST day |
|---|---|---|
| `"D"` | Local calendar day | 23 or 25 hours |
| `"24h"` | Fixed 24 hours of elapsed time | Always 24 hours |
| `"MS"`, `"W"` | Local calendar month or week | Follows the local calendar |
Data stored in UTC
Many systems store timestamps in UTC. If you resample UTC data directly, your "days" run from 00:00 UTC, which is not a local day. Convert to the local zone first, then resample.
import pandas as pd
utc_idx = pd.date_range("2024-03-09", periods=72, freq="h", tz="UTC")
s = pd.Series(1, index=utc_idx)
local = s.tz_convert("America/New_York").resample("D").sum()
print(local)The first and last bins are partial, because the UTC range does not line up with local midnight. The result has bins for 03-08 (5 rows), 03-09 (24), 03-10 (23) and 03-11 (20).
Naive timestamps that need a zone
If your index has no zone, attach one with tz_localize() before resampling. Two kinds of local times cause errors:
- Ambiguous times occur when clocks fall back and 01:30 happens twice. Pass
ambiguous="infer", or a boolean array that marks the DST occurrences, to resolve them. - Nonexistent times occur when clocks spring forward and 02:30 never happens. Pass
nonexistent="shift_forward"or"NaT"to handle them.
naive = pd.date_range("2024-11-02", periods=4, freq="D")
print(naive.tz_localize("America/New_York"))This example is safe because midnight is never ambiguous in this zone. Hourly data that crosses a DST change usually needs the arguments above.
Checklist
- Make sure the index has the zone you want your bins to follow.
- Use
"D","W"or"MS"for local calendar bins. - Expect 23-hour and 25-hour days in sums, and use
mean()if you want a value that does not depend on the day length. - Check bin counts around DST dates, for example with
s.resample("D").count().
Exercise
An hourly series in America/New_York covers 2024-11-03, the day clocks fall back. What does resample("D").count() report for that day, and why?
Show answer
It reports 25. The clocks go back from 02:00 to 01:00, so the hour from 01:00 to 02:00 happens twice. The local calendar day then lasts 25 hours, and resample("D") follows the local calendar.