Skip to content

date_trunc on time zone aware timestamps is an hour off when a DST change falls inside the bucket #7720

Open

Description

@jonasdedden

Environment: daft 0.7.26, Python 3.14, Linux. The code involved is unchanged on main (865feff).

import datetime as dt
import daft
import daft.functions as F
from daft import col

df = daft.from_pydict(
    {"t": [dt.datetime(2020, 3, 29, 10), dt.datetime(2020, 10, 25, 10), dt.datetime(2020, 6, 1, 10)]}
).select(F.replace_time_zone(col("t"), "Europe/Berlin").alias("t"))
print(df.select(F.date_trunc("1 day", col("t"))).to_pydict())

Actual: on the two DST days, the result is not local midnight.

2020-03-28 23:00:00+01:00   # previous day
2020-10-25 01:00:00+02:00
2020-06-01 00:00:00+02:00   # correct, no DST change in the bucket

Expected: local midnight, 2020-03-29 00:00:00+01:00 and 2020-10-25 00:00:00+02:00. That's what Polars returns (dt.truncate("1d")), as does pandas (dt.floor("D")).

The same happens for any bucket that contains a DST change, e.g. date_trunc("7 hours", ...) around a transition. Buckets of up to an hour aren't affected for zones whose transitions fall on whole hours.

Cause: for time-zone-aware input, truncate truncates the naive local time, but then subtracts the same amount as a fixed Duration from the original instant. When the UTC offset changes between the truncated local time and the original, the subtraction overshoots or undershoots by the offset difference. (#7715 keeps this approach, so it doesn't fix this.)

Suggested fix: truncate the naive local time and then localize it (resolve the local result back to an instant in the time zone). For ambiguous or nonexistent results, Polars keeps the input's offset first, then falls back to the offset at the guessed instant.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions