Skip to content

Raise when a time cannot be represented in nanoseconds - #1290

Merged
d-chambers merged 4 commits into
devfrom
time-ns-range
Oct 1, 2026
Merged

d-chambers merged 4 commits into
devfrom
time-ns-range

Conversation

@d-chambers

Copy link
Copy Markdown
Contributor

Description

Fixes #890. Replaces #927 by @manduinca, whose PR mapped every spelling that wrapped and whose cases the tests here reuse; thank you.

dc.to_datetime64 and dc.to_timedelta64 silently wrapped or saturated values outside the nanosecond range: to_datetime64("2500-01-01") returned 1915-06-14, np.array([2e10]) saturated, "2500" (parsed in years) wrapped through the calendar. They now raise TimeOverflowError, which is both a TimeError and an OverflowError, so existing handlers keep working.

How:

  • Every datetime64/timedelta64 is widened to ns through one helper, _as_ns, which bounds the integer count for its unit before casting. Numpy 2.5 raises on such casts but 2.3.3 (our floor) wraps, so the bound is explicit. Datetime years and months go through days first.
  • Text that numpy parses straight into ns or finer (sub-microsecond digits) is checked against the same text parsed in whole seconds; more than nine fractional digits still truncate to ns.
  • Seconds given as numbers are bounded before the multiply.
  • date input now goes through the string path, replacing its own check.
  • pd.Timestamp, pd.Timedelta and duration strings now come back in ns rather than their own unit.
  • spool.select with an unrepresentable date string bound raises instead of falling back to a string comparison that matched nothing.

Behaviour changes worth knowing:

  • A file whose time array holds a single out-of-range sentinel now fails to read rather than producing wrapped or saturated times.
  • patch.select(time=(None, "3000-01-01")) raises; on dev it silently returned an empty patch.

Not covered: pandas string arrays parse with errors="coerce", so a value pandas itself cannot hold becomes NaT as before (catching it needs a per-element reparse); a list mixing datetime64 units is promoted by numpy before conversion; Python timedeltas beyond ~292,000 years wrap inside numpy's own constructor.

Performance: patch.select is unchanged (~360 µs). Scalar conversions of strings and non-ns datetimes cost ~2.5 µs instead of ~0.4 µs; ns scalars ~0.5 µs.

Tested on numpy 2.5.3 and 2.3.3.

Changelog

  • fixed breaking: dc.to_datetime64 and dc.to_timedelta64 raise TimeOverflowError for a time outside the nanosecond range instead of returning a wrapped or saturated one.

Checklist

I have:

  • filled in the Changelog section above (see dascore/docs/contributing/general_guidelines.qmd).

I have (if applicable):

  • referenced the GitHub issue this PR closes.
  • documented the new feature with docstrings and/or appropriate doc page.
  • included tests. See testing guidelines.
  • added the "ready_for_review" tag once the PR is ready to be reviewed.

@coderabbitai

coderabbitai Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 8f86b4d4-3e57-4ab0-b1e9-de9188758ea2

📥 Commits

Reviewing files that changed from the base of the PR and between ebccde4 and 5322609.

📒 Files selected for processing (6)
  • dascore/core/inventory_loader.py
  • dascore/exceptions.py
  • dascore/io/index/query.py
  • dascore/utils/time.py
  • tests/test_io/test_index/test_time_query.py
  • tests/test_utils/test_time.py
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T15:01:25.124909Z 48fe281 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 48fe281db4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread dascore/utils/time.py Resolved
@d-chambers d-chambers added the benchmark Run the benchmark suite label Oct 1, 2026
@codecov

codecov Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ebccde4) to head (efaa916).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff            @@
##               dev     #1290   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          225       225           
  Lines        36589     36644   +55     
=========================================
+ Hits         36589     36644   +55     
Flag Coverage Δ
network 35.18% <56.09%> (+0.03%) ⬆️
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed

codspeed Bot commented Oct 1, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 112 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing time-ns-range (efaa916) with dev (ebccde4)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@d-chambers
d-chambers merged commit 682e830 into dev Oct 1, 2026
17 of 19 checks passed
@d-chambers
d-chambers deleted the time-ns-range branch October 1, 2026 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmark Run the benchmark suite ready_for_review PR is ready for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant