Skip to content

BUG: Timedelta kwargs fall back to coarser resolution on overflow#65168

Open
jbrockmendel wants to merge 2 commits intopandas-dev:mainfrom
jbrockmendel:api-46587
Open

BUG: Timedelta kwargs fall back to coarser resolution on overflow#65168
jbrockmendel wants to merge 2 commits intopandas-dev:mainfrom
jbrockmendel:api-46587

Conversation

@jbrockmendel
Copy link
Copy Markdown
Member

Summary

  • pd.Timedelta(days=365_000) and similar kwargs that overflow nanosecond int64 bounds now fall back to coarser resolutions (us, ms, s) instead of raising OutOfBoundsTimedelta
  • The common (non-overflow) path is unchanged — the fallback only triggers on OverflowError

closes #46587

Test plan

  • Existing timedelta scalar tests pass (559 passed)
  • New tests for kwargs that overflow ns but fit in coarser resolutions
  • Truly out-of-bounds values (e.g. days=10**15) still raise
  • Existing behavior preserved: Timedelta(nanoseconds=1000).unit == "ns" (GH#63216), small-value kwargs still get us resolution

🤖 Generated with Claude Code

Comment on lines +2205 to +2208
int(ns)
+ int(us * 1_000)
+ int(ms * 1_000_000)
+ seconds
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Could we pull this and the one in the try block as a total_ns variable before the try?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

will update

Comment on lines +2210 to +2211
if total_ns % 1_000 != 0:
reso_value, reso_abbrev = total_ns, "ns"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This if is equivalent to what happens in the try block above correct? We could just replace the above try with this if/else first right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

i think they're different in the non-overflow case. e.g. Timedelta(days=1) would give unit="s" if we always use this cascade, whereas we want to default to unit="us".

jbrockmendel and others added 2 commits April 13, 2026 15:27
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API/DES: Non-Nanosecond Tracker

2 participants