Skip to content

feat: Implement dt.truncate and dt.offset_by - #152

Open
jonasdedden wants to merge 4 commits into
narwhals-dev:mainfrom
jonasdedden:feat/dt-truncate-offset-by
Open

jonasdedden wants to merge 4 commits into
narwhals-dev:mainfrom
jonasdedden:feat/dt-truncate-offset-by

Conversation

@jonasdedden

@jonasdedden jonasdedden commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Implements dt.truncate and dt.offset_by, parsing every/by with narwhals' Interval, so invalid intervals and multiples raise the usual errors.

dt.truncate:

  • ns … d go through F.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 a Timestamp[us] and cast back.
  • mo / q / y floor a month index (year * 12 + month - 1) to the multiple and rebuild the date with make_date, then cast back to the input dtype. For tz-aware input that gives local midnight.
  • date_trunc panics 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 to ns first.

dt.offset_by:

  • ns … h add 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 to ns.
  • d / mo / q / y shift the local wall time, so 1d keeps the time of day across a DST change while 24h doesn't, like Polars. Months use F.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_months return 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/Amsterdam across both DST transitions. Results match, except where pyarrow doesn't support month offsets or returns datetimes for Date + 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 from run_tests.py.

Update:

  • DST handling: offset_by (d/mo/q/y) on tz-aware columns raised in Daft's replace_time_zone whenever 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:
    • ambiguous results keep the input's UTC offset;
    • results in a gap are moved across it.
  • date_trunc on 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. 1d on DST days), so buckets longer than an hour now truncate the local time and re-attach the zone.
  • Checked against Polars: 20k random values each for naive, Europe/Berlin, Asia/Kathmandu, America/Santiago and 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.
  • Faster 1mo/1y truncation: it now steps back with date_sub instead of make_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.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ce4c8187-f6c3-42f0-a910-427400e119a6
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@jonasdedden
jonasdedden force-pushed the feat/dt-truncate-offset-by branch 3 times, most recently from 4a90466 to 42651f4 Compare November 15, 2026 14:51
…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
jonasdedden force-pushed the feat/dt-truncate-offset-by branch from 42651f4 to c08fb15 Compare November 15, 2026 15:26

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant