Sub topic 9
Calendar days versus 24-hour steps
This chapter assumes you can create tz-aware timestamps with tz_localize, convert them with tz_convert, and recognize the ambiguous and nonexistent local times that daylight saving time (DST) produces. It also assumes you know what a Timedelta is. The chapter covers the case where a "day" is not always 24 hours long, and which pandas tools count calendar days and which count elapsed time.
Two meanings of "one day"
In a zone with DST, "one day later" can mean two different things:
- Calendar day: the same wall-clock time on the next date. Noon today becomes noon tomorrow, even if the clocks changed overnight.
- Fixed duration: exactly 24 hours of elapsed time. Noon today becomes 11:00 or 13:00 tomorrow if the clocks moved by an hour.
In America/New_York, 2024-03-10 lasts 23 hours because clocks jump from 02:00 to 03:00. 2024-11-03 lasts 25 hours because the 01:00 hour happens twice. On every other day the two meanings agree, so code that mixes them up usually passes tests and then fails on one or two dates a year.
Both meanings are valid. A daily report wants calendar days. A sensor sampled every 86,400 seconds wants fixed durations. The bug is picking one while meaning the other.
Ranges: "D" versus "24h"
pd.date_range with a time zone shows the difference directly. The two ranges below start on the same date and have the same length and nominally the same step.
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)
# 2024-03-09 00:00:00-05:00
# 2024-03-10 00:00:00-05:00
# 2024-03-11 00:00:00-04:00
print(fixed)
# 2024-03-09 00:00:00-05:00
# 2024-03-10 00:00:00-05:00
# 2024-03-11 01:00:00-04:00"D" keeps midnight and lets the UTC offset change from -05:00 to -04:00. "24h" keeps the elapsed time between points constant, so the third point lands at 01:00 local time. Nothing is wrong with either result. They answer different questions.
How pandas builds the range
For calendar frequencies, pandas generates the sequence in local wall-clock time and then attaches the zone to each point. For fixed frequencies, it steps in absolute time. That explains why the "D" result has midnight on every row but the gaps between rows are 24, 23 and then 24 hours.
Which frequencies are which
| Frequency alias | Step type | Behavior across DST |
|---|---|---|
| `"D"` in `date_range` with `tz` | Calendar day | Same wall-clock time each date |
| `"W"`, `"MS"`, `"B"` | Calendar | Same wall-clock time on the target date |
| `"24h"`, `"h"`, `"min"`, `"s"` | Fixed duration | Constant elapsed time, wall-clock time shifts |
When you only need fixed steps, the safest construction is to build the range in UTC and convert for display. UTC has no DST, so nothing can drift.
import pandas as pd
tz = "America/New_York"
fixed_utc = pd.date_range("2024-03-09 05:00", periods=3, freq="24h", tz="UTC")
print(fixed_utc.tz_convert(tz))
# 2024-03-09 00:00:00-05:00
# 2024-03-10 00:00:00-05:00
# 2024-03-11 01:00:00-04:00This gives the same instants as the "24h" range above, with no zone-specific behavior to reason about.
Single timestamps: Timedelta versus DateOffset
Adding to one timestamp has the same split. A pd.Timedelta is a fixed amount of elapsed time. A pd.DateOffset is a calendar amount.
import pandas as pd
tz = "America/New_York"
start = pd.Timestamp("2024-03-09 12:00", tz=tz)
fixed = start + pd.Timedelta(days=1)
calendar = start + pd.DateOffset(days=1)
print(fixed) # 2024-03-10 13:00:00-04:00
print(calendar) # 2024-03-10 12:00:00-04:00
print(calendar - start) # 0 days 23:00:00Timedelta(days=1) is the same as Timedelta(hours=24), so the result is 13:00 on the wall clock. DateOffset(days=1) moves the date and keeps 12:00. Subtracting the original from the calendar result shows that only 23 hours elapsed.
Subtracting two aware timestamps always returns elapsed time, as the arithmetic chapter showed. That is why calendar - start reports 23 hours instead of one day.
Measuring how long each day really is
You can check how many hours each local day contains by taking differences between consecutive local midnights.
import pandas as pd
tz = "America/New_York"
for first in ["2024-03-09", "2024-11-02"]:
midnights = pd.date_range(first, periods=4, freq="D", tz=tz)
gaps = pd.Series(midnights).diff().dropna()
print(first, (gaps.dt.total_seconds() / 3600).tolist())
# 2024-03-09 [24.0, 23.0, 24.0]
# 2024-11-02 [24.0, 25.0, 24.0]The 23.0 and 25.0 values are the short and long days. This check is also a cheap test for your own pipelines. If a daily table has a day with 23 or 25 hourly rows, that is correct, not missing or duplicated data.
Hourly ranges show this too. Fixed steps produce a different number of rows per day:
import pandas as pd
tz = "America/New_York"
spring = pd.date_range("2024-03-10", "2024-03-11", freq="h", tz=tz, inclusive="left")
fall = pd.date_range("2024-11-03", "2024-11-04", freq="h", tz=tz, inclusive="left")
normal = pd.date_range("2024-06-01", "2024-06-02", freq="h", tz=tz, inclusive="left")
print(len(spring), len(fall), len(normal)) # 23 25 24If a downstream consumer assumes 24 rows per day, the spring and fall days break it. Decide how to handle those days instead of padding or dropping rows.
Start times that do not exist on every date
A calendar range reuses the same wall-clock time each day, and that time may not exist on a transition day. A start of 02:30 is valid on most days, but 2024-03-10 02:30 is skipped in New York. Build the naive range first, then localize it with a rule for the skipped time. The localization step is the one from the DST edge-case chapter.
import pandas as pd
tz = "America/New_York"
naive = pd.date_range("2024-03-09 02:30", periods=3, freq="D")
aware = naive.tz_localize(tz, nonexistent="shift_forward")
print(aware)
# 2024-03-09 02:30:00-05:00
# 2024-03-10 03:00:00-04:00
# 2024-03-11 02:30:00-04:00The middle row moved from 02:30 to 03:00, the first valid instant after the gap. That is a choice. nonexistent="NaT" would drop the row to a missing value, and a Timedelta would shift it by a fixed amount. Pick the rule that fits your data, and document it.
The reverse problem exists in autumn. A calendar range with a wall-clock time in the repeated hour, such as 01:30 on 2024-11-03, is ambiguous, and you need the ambiguous argument when localizing. The same two-step pattern applies: build naive, then localize with an explicit rule.
Choosing between them
The right choice depends on what the data represents, not on which call looks shorter.
Use these rules of thumb:
- Daily business data such as sales per day, shifts, or "every morning at 09:00" needs calendar steps. A shift at 09:00 stays at 09:00 after the clocks change.
- Machine data such as sensor readings, heartbeats, or counters that tick every N seconds needs fixed steps. The sensor does not know about DST.
- Mixed requirements are common. Store timestamps in UTC as fixed instants, and compute local calendar boundaries only when you group or report.
Common mistakes
- Using
Timedelta(days=1)to get "tomorrow" in local time. The result is correct on 363 or so days a year and an hour off on the others. - Assuming
freq="D"andfreq="24h"are interchangeable. They are interchangeable only in a naive index or in UTC, where no DST exists. - Dividing a duration by 24 hours to count days. Across a DST change,
calendar - startabove is 23 hours, so a count based on hours reports less than a full day. - Hard-coding 24 rows per day. Spring days have 23 hourly rows and fall days have 25.
- Testing only in summer. Put 2024-03-10 and 2024-11-03 (or the transition dates of your zone) in your test data.
The next chapter applies this distinction to resampling and grouping. resample("D") on a tz-aware index follows local calendar days, so the 23-hour and 25-hour days appear there as well.
Exercise
A tz-aware index in America/NewYork is built with `pd.daterange("2024-11-02", periods=3, freq="24h", tz="America/New_York")`. What are the three local timestamps, and what would you change to get midnight on each of the three dates?
Show answer
The index is 2024-11-02 00:00-04:00, 2024-11-03 00:00-04:00 and 2024-11-03 23:00-05:00. The third point is still on 11-03 because 11-03 has 25 hours, so exactly 24 hours after midnight on 11-03 is 23:00. Change freq="24h" to freq="D" to get midnight on 11-02, 11-03 and 11-04, with offsets -04:00, -04:00 and -05:00.
Exercise
You add one day to pd.Timestamp("2024-03-09 12:00", tz="America/New_York") once with pd.Timedelta(days=1) and once with pd.DateOffset(days=1). What are the two results, and how much time elapses in each case?
Show answer
Timedelta(days=1) gives 2024-03-10 13:00:00-04:00 after exactly 24 hours. DateOffset(days=1) gives 2024-03-10 12:00:00-04:00 after only 23 hours, because the 02:00 to 03:00 skip removed an hour from that day.
Exercise
You need a series of daily timestamps at 02:30 local time in America/New_York for 2024-03-09 through 2024-03-11, with no errors. How do you build it, and what happens to the 2024-03-10 entry?
Show answer
Build a naive range with pd.date_range("2024-03-09 02:30", periods=3, freq="D") and call .tz_localize("America/New_York", nonexistent="shift_forward"). The 2024-03-10 entry does not exist locally, so it moves to 03:00-04:00, the first valid time after the gap. The other two entries stay at 02:30.