Repository navigation
fix(indexing): select coarse datetime64 slices beyond ns range (#10975) - #11927
Conversation
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
headtr1ck
left a comment
There was a problem hiding this comment.
Thanks @aniruddhaadak80! I pushed a small fix and merged main.
np.datetime64(stop, unit) only gives the first instant of a partial string, but pandas includes the whole period, e.g. slice("2400-03", "2400-03") selected only 2400-03-01T00:00:00 instead of all of March. The stop bound is now parsed at the string's own precision and moved to the end of its period, like pandas' DatetimeIndex._parsed_string_to_bounds does. Since the Gregorian calendar repeats every 400 years, I checked that selections in 2400 now match the same selections in 2000.
[This is Claude Code on behalf of Michael Niklas]
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Description
Closes #10975.
DataArray.sel/Dataset.selwith a string slice (e.g.slice("2400-01-01", "2420-01-01")) on adatetime64index whose unit is coarser than nanoseconds raisedOutOfBoundsDatetime, even when every bound is perfectly representable in the index's unit:This is the shape of index produced by
CFDatetimeCoder(time_unit="s"), so it is a realistic path for data beyond pandas' nanosecond range.The failure comes from
_query_sliceinxarray/core/indexes.py, which passes the slice bounds straight topandas.Index.slice_indexer. For string bounds pandas parses them at nanosecond resolution and overflows for dates outside 1677–2262, regardless of the index's own resolution. Passingnp.datetime64scalars in the index's unit avoids that parsing:DatetimeIndex(["2400-01-01", ...], dtype="datetime64[s]").slice_indexer(np.datetime64("2400-01-01", "s"), np.datetime64("2420-01-01", "s"))returns the correct slice.The fix catches
OutOfBoundsDatetime, and only for aDatetimeIndexwhose dtype is notdatetime64[ns], re-tries with string bounds converted to the index's unit. All other cases (including nanosecond indexes) keep the current behaviour and error. No change to public API.A
doc/whats-new.rstentry was added under the unreleased "Bug Fixes" section.Checklist
.sel()fails ondatetime64[s]object #10975whats-new.rstapi.rst(no new functions or methods)Tests
Regression test in
xarray/tests/test_indexes.pyparametrized overdatetime64[s],[ms]and[us], building a monthly index spanning 2390–2409 and selecting a slice in 2400–2402.Actual results in a worktree-local environment (Python 3.12, NumPy 2.5.3, pandas 3.0.6, Windows). A local
sitecustomize.pywas used onPYTHONPATHto give the tagless editabledaskcheckout a parseable__version__; nothing from it is in the diff.The 9 failures are pre-existing environment failures, unrelated to this change: pandas 3.0.6 refuses to import its optional xarray integration because this tagless editable install reports
xarray 0.0.0(ImportError: Pandas requires version '2024.10.0' or newer of 'xarray'). They are all categorical/from_dataframe/extension-array tests and fail the same way without the fix.Lint/format:
Limitations
OutOfBoundsDatetime. This deliberately does not change semantics for in-range strings, where pandas' partial-string period handling (e.g."2400"meaning the whole year) is preserved. For out-of-range partial strings the fallback uses the start of the parsed period, which is the only representation pandas cannot provide itself.cftimeindexes andPeriodIndexare unaffected.AI Disclosure
Tools: OpenCode (AI coding agent)
An AI coding agent located the failure in
_query_slice, drafted the fallback and the regression test, and ran the verification above. I reviewed the diff and each claimed result.