Sub topic 9
Calendar days versus 24-hour steps
This chapter assumes you can already localize timestamps with tz_localize and know that daylight saving time (DST) shifts the UTC offset of a zone. Here you will see how that shift affects "one day later", and how to pick the right kind of step.
Two meanings of "one day"
A calendar day moves the date forward by one and keeps the local clock time. A fixed duration moves forward by exactly 24 hours of elapsed time. Most days these are the same. On a DST transition day they are not:
- In
America/New_York, 2024-03-10 lasted 23 hours because the clocks jumped from 02:00 to 03:00. - On 2024-11-03 the day lasted 25 hours because the clocks fell back from 02:00 to 01:00.
pandas lets you choose either meaning. The frequency string you pass decides which one you get.
Ranges across a spring-forward change
Build two ranges that start the day before the March 2024 change in New York:
import pandas as pd
tz = "America/New_York"
daily = pd.date_range("2024-03-09", periods=3, freq="D", tz=tz)
fixed = pd.date_range("2024-03-09", periods=3, freq="24h", tz=tz)
print(daily) # midnight each day: -05:00, -05:00, -04:00
print(fixed) # 00:00-05:00, 00:00-05:00, then 01:00-04:00The "D" range stays on local midnight. The offset changes from -05:00 to -04:00, so the gap between the second and third entries is only 23 hours. The "24h" range keeps every gap at exactly 24 hours. Because one day is 23 hours long, the third timestamp lands at 01:00 local time.
Single timestamps: Timedelta versus DateOffset
The same split applies when you add to one timestamp.
import pandas as pd
start = pd.Timestamp("2024-03-10 00:00", tz="America/New_York")
print(start + pd.Timedelta(days=1)) # 2024-03-11 01:00:00-04:00
print(start + pd.DateOffset(days=1)) # 2024-03-11 00:00:00-04:00pd.Timedelta(days=1) is a fixed length of 24 hours. pd.DateOffset(days=1) is a calendar step, so it keeps the local time.
| Tool | Meaning of one day | Local clock time after DST | Typical use |
|---|---|---|---|
| `freq="D"`, `DateOffset(days=1)` | Next calendar date | Preserved | Daily reports, midnight boundaries |
| `freq="24h"`, `Timedelta(days=1)` | Exactly 24 elapsed hours | Can shift by an hour | Sensor spacing, durations |
Measuring the length of a day
Calendar steps give you days of unequal length. You can see this by subtracting consecutive entries:
import pandas as pd
days = pd.date_range("2024-03-09", periods=4, freq="D", tz="America/New_York")
print(days.to_series().diff())The output shows 1 days 00:00:00, 0 days 23:00:00, 1 days 00:00:00. The 23-hour gap is the transition day.
Common errors
- Building the range in UTC, then converting. A
24hrange in UTC is evenly spaced. Aftertz_convert("America/New_York")its local times drift away from midnight. If you want local midnights, build the range withtz=set directly andfreq="D". - Using a naive range, then localizing.
pd.date_range("2024-03-09", periods=3, freq="D").tz_localize(tz)also gives local midnights. It can fail if a midnight does not exist in the zone, which happens in some zones that change clocks at midnight. Passingtz=todate_rangehandles those cases more safely. - Assuming a day has 24 hours. Code that divides elapsed time by 24 hours to count days will be off on transition days. Count calendar dates instead.
Exercise
You build pd.date_range("2024-11-03", periods=2, freq="D", tz="America/New_York"). On 2024-11-03 the clocks fall back, so that day lasts 25 hours. How many hours separate the two timestamps, and what would change if you used freq="24h"?
Show answer
The two timestamps are 25 hours apart, from 2024-11-03 00:00-04:00 to 2024-11-04 00:00-05:00. With freq="24h" the gap would be exactly 24 hours, so the second timestamp would be 2024-11-03 23:00-05:00 instead of local midnight.