Repository navigation
feat: Implement dt.truncate and dt.offset_by - #152
Open
jonasdedden wants to merge 4 commits into
Open
jonasdedden wants to merge 4 commits into
jonasdedden wants to merge 4 commits into
Conversation
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
jonasdedden
force-pushed
the
feat/dt-truncate-offset-by
branch
3 times, most recently
from
November 15, 2026 14:51
4a90466 to
42651f4
Compare
…mo/1y truncate - Attaching the time zone back to a shifted/truncated local time now follows Polars instead of raising in Daft's replace_time_zone: an ambiguous result keeps the input's UTC offset, a result in a DST gap is moved across it. - Daft's date_trunc shifts the original instant by the truncated duration, which is an hour off when a DST change falls inside the bucket; buckets longer than an hour now truncate the local time and attach the zone again. - 1mo/1y truncation steps back with date_sub instead of make_date (about 1.5x/2x faster).
jonasdedden
force-pushed
the
feat/dt-truncate-offset-by
branch
from
November 15, 2026 15:26
42651f4 to
c08fb15
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements
dt.truncateanddt.offset_by, parsingevery/bywith narwhals'Interval, so invalid intervals and multiples raise the usual errors.dt.truncate:ns…dgo throughF.date_trunc. Its epoch-aligned grid matches Polars (e.g.13d→ 2021-02-23), and tz-aware input is truncated in local time. Date input is truncated via aTimestamp[us]and cast back.mo/q/yfloor a month index (year * 12 + month - 1) to the multiple and rebuild the date withmake_date, then cast back to the input dtype. For tz-aware input that gives local midnight.date_truncpanics for steps finer than the column's time unit (date_trunc panics (remainder with a divisor of zero) for intervals finer than the timestamp's time unit Eventual-Inc/Daft#7713). Such a step is a no-op when it divides the unit; otherwise the column is cast tonsfirst.dt.offset_by:ns…hadd a duration in the column's own time unit, so the dtype is kept. If the offset isn't representable in that unit, the column is cast tons.d/mo/q/yshift the local wall time, so1dkeeps the time of day across a DST change while24hdoesn't, like Polars. Months useF.add_months, which clamps to the end of the month (Jan 31 + 1mo = Feb 28). It returns a Date, so the time of day is added back for datetimes.Workaround: with a literal argument, Daft's
make_date/add_monthsreturn the first row's result for every row (Eventual-Inc/Daft#7712). The month count and the day are therefore passed as column-shaped constants (_column_lit). This can go once a Daft release with the fix is the minimum.Beyond the test suite, I compared against narwhals' pyarrow backend for nulls, pre-1970 values, Date input, negative offsets and
Europe/Amsterdamacross both DST transitions. Results match, except where pyarrow doesn't support month offsets or returns datetimes forDate + 2d; there Daft follows Polars.Stacked on #149, which adds
convert_time_zone(used by the tz tests). On top of it: 45 more cases pass, no new failures, and 9 names dropped fromrun_tests.py.Update:
offset_by(d/mo/q/y) on tz-aware columns raised in Daft'sreplace_time_zonewhenever the result was ambiguous or fell into a DST gap. The time zone is now re-attached the way Polars does it, so it never raises:date_truncon tz-aware columns: Daft truncates the local time but then shifts the original instant by the truncated duration. That is an hour off when a DST change falls inside the bucket (e.g.1don DST days), so buckets longer than an hour now truncate the local time and re-attach the zone.Europe/Berlin,Asia/Kathmandu,America/Santiagoand Date columns, across 18 intervals. All 78 combinations match. The exception is where Polars itself raises (nonexistent local midnight in Kathmandu); there this returns a value.1mo/1ytruncation: it now steps back withdate_subinstead ofmake_date(55 → 41 ms and 57 → 32 ms per 5M rows), and planning cost drops about 3×.Note
I used Claude (Opus 5.5) to help with these changes and this description.