ENH: raise OverflowError on timedelta64 float multiply/divide overflow#31979
Open
jbrockmendel wants to merge 1 commit into
Open
ENH: raise OverflowError on timedelta64 float multiply/divide overflow#31979jbrockmendel wants to merge 1 commit into
jbrockmendel wants to merge 1 commit into
Conversation
Multiplying or dividing a timedelta64 by a float previously cast the float64 result straight to int64, silently saturating (or landing on the NaT sentinel) when the value exceeded int64 range. Detect that in the TIMEDELTA_md_m_multiply, TIMEDELTA_dm_m_multiply and TIMEDELTA_md_m_divide loops and raise OverflowError instead, mirroring the integer multiply/add/subtract loops (numpy numpyGH-31378). A shared timedelta_from_double helper maps a NaN result to NaT and reports any magnitude >= 2**63 (finite overflow or +/-inf) as overflow; the bound is 2**63 rather than int64.max because 2**63 - 1 is not representable in float64. Special cases are preserved: a NaN operand, 0 * inf, division by zero, and division by infinity still yield NaT (or zero) rather than raising. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
61d19a0 to
43c78f5
Compare
Member
|
sorry but this goes against the AI policy |
Contributor
Author
|
Fair enough. You closed while I was in the process of updating the comment with the ping to seberg acknowledging the AI use. |
Member
|
Ah ok, that changes things |
Member
|
I'm still not sure how I feel about Claude as co-author though. |
Member
IMO it's mostly advertising for anthropic but it shouldn't stop us from accepting the code. AI-generated code will get in either way. |
Member
|
Also sorry for the drive-by comment, I think improvements like this are great and I'll try to get it reviewed. |
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.
Human here: @seberg as with my other recent PRs, acknowledging this is mostly claude. This is porting pandas-dev/pandas#65515
Claude below the line
Follow-up to GH-31378, which made
datetime64/timedelta64add, subtract, and integer-multiply raiseOverflowErroron int64 overflow instead of silently wrapping. This extends the same guarantee to the float multiply and divide loops, which previously cast thefloat64result straight toint64and silently saturated (or landed on theNaTsentinel) when the value exceeded int64 range.What changes
TIMEDELTA_md_m_multiply(td * float),TIMEDELTA_dm_m_multiply(float * td), andTIMEDELTA_md_m_divide(td / float, which also backstd // float) now raiseOverflowErrorwhen the result would overflow int64:A shared
timedelta_from_doublehelper maps aNaNresult toNaTand treats any magnitude>= 2**63(finite overflow or±inf) as overflow. The bound is2**63rather thanint64.max, because2**63 - 1is not exactly representable infloat64and rounds up to2**63; a result landing there would otherwise slip past the check and saturate on the cast (and-2**63is itself theNaTsentinel).Preserved (non-overflow) behavior
A
NaNoperand,0 * inf, division by zero, and division by infinity still yieldNaT(or zero), matching the pre-existing convention:The one intentional behavior change to an existing test:
timedelta64(nonzero) * infnow raises instead of returningNaT.🤖 Generated with Claude Code