Raise OutOfBoundsDatetime for datetime conversions that exceed the target unit's range - #23216
Merged
rapids-bot[bot] merged 3 commits intoJul 14, 2026
Merged
Conversation
…rget unit's range Forcing values beyond the target resolution's representable range (e.g. constructing with dtype='datetime64[ns]' from year-2263 data, or astype from a coarser unit) silently wrapped the underlying int64 and produced garbage timestamps. pandas bounds-checks every narrowing conversion (astype_overflowsafe) and raises pd.errors.OutOfBoundsDatetime. - DatetimeColumn.as_datetime_column bounds-checks narrowing casts on the column's integer view (a host round-trip through pd.Timestamp can itself overflow for extreme values) and raises with a pandas-matching message. The check is relative to the target unit, so e.g. year 9999 still fits in datetime64[s] and datetime64[us]. - StringColumn.strptime parses to seconds first for sub-second targets and rejects values whose whole-second part falls outside the target unit's range; overflows of less than a second inside the boundary second are not detected, but in-bounds values are never rejected. - to_datetime's dual ns/us %f parse falls back to microsecond precision when the nanosecond parse raises, matching pandas' unit inference. - The tz transition-table casts in tz_localize/_local_time use the raw cast: those tables contain sentinel entries beyond the finer units' bounds that intentionally wrap around. - can_cast_safely now checks the negative bound (pre-epoch values were never checked) and explicitly treats coarser-resolution casts as unsafe: callers (replace, join key matching) rely on 'safely' meaning lossless, which the old wrapped-bound formula provided accidentally. - Scalar .loc lookups with an out-of-range datetime key raise KeyError like pandas; list keys raise OutOfBoundsDatetime in both. - Remove 16 pandas-testing plugin entries for tests/base/test_constructors.py::test_constructor_datetime_outofbound, which now passes. - Add tests covering constructors (Series/Index/DataFrame), astype (including tz and string->tz), to_datetime, replace, .loc, and can_cast_safely.
Contributor
Author
|
/okay to test 69fbab8 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
💤 Files with no reviewable changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughChangesDatetime safety and pandas compatibility
Estimated code review effort: 4 (Complex) | ~45 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
mroeschke
reviewed
Jul 10, 2026
Contributor
Author
|
/okay to test 48cfadd |
mroeschke
approved these changes
Jul 13, 2026
Contributor
Author
|
/merge |
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.
Description
cuDF silently wrapped the underlying int64 values when a datetime conversion could not fit the target resolution — e.g.
cudf.Series(np.array(["2263-01-01"], dtype="datetime64[D]"), dtype="datetime64[ns]")producedTimestamp('1678-06-12 00:25:26.290448384'). pandas bounds-checks every narrowing conversion (astype_overflowsafe) and raisespd.errors.OutOfBoundsDatetime. This PR makes cuDF raise the same error, fixing all 16 xfailed parametrizations oftests/base/test_constructors.py::test_constructor_datetime_outofboundin the pandas test suite (Series/Index/DataFrame constructors ×datetime64[D]array, object arrays ofdatetime.datetime,np.datetime64, and strings).Changes:
DatetimeColumn.as_datetime_columnbounds-checks unit-narrowing casts on the column's integer view (a host round-trip throughpd.Timestampcan itself overflow for extreme values) and raises with a pandas-matching message. The check is relative to the target unit: year 9999 still fits indatetime64[s]/datetime64[us], just not nanoseconds.StringColumn.strptimeparses to seconds first for sub-second datetime targets and rejects values whose whole-second part falls outside the target unit's range (libcudf otherwise parses straight into wrapped int64). Overflows of less than a second inside the boundary second are not detected, but in-bounds values are never rejected.to_datetime's dual ns/us%fparse falls back to microsecond precision when the nanosecond parse raises, matching pandas' unit inference. Nanosecond-precision strings beyond the ns range (e.g."2263-01-01 00:00:00.123456789") now raise like pandas instead of returning wrapped values.tz_localize/_local_timeswitch to the rawcast: those tables contain sentinel entries beyond the finer units' bounds that intentionally wrap around.can_cast_safelynow checks the negative bound (pre-epoch values were never checked, so e.g.1600-01-01in ms reported as safely castable to ns) and explicitly treats coarser-resolution casts as unsafe: callers (replace, join key matching) rely on "safely" meaning lossless, which the old wrapped-bound formula only provided accidentally..loclookups with an out-of-range datetime key raiseKeyErrorlike pandas; list keys raiseOutOfBoundsDatetimein both.Known minor divergence: when the target dtype is a
pd.ArrowDtypetimestamp, cuDF raisesOutOfBoundsDatetimewhere pandas surfacespyarrow.lib.ArrowInvalid; raising is still strictly better than the previous silent wraparound.Tests added cover constructors (5 input flavors × Series/Index/DataFrame),
astype(above/below range, with nulls, all-null, tz-aware, string→tz),to_datetime,replacewith sub-resolution values,.loc, andcan_cast_safely(negative bound, coarser-resolution).Checklist