ENH: raise OverflowError on timedelta64 float multiply/divide overflow#31979
ENH: raise OverflowError on timedelta64 float multiply/divide overflow#31979jbrockmendel 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
|
sorry but this goes against the AI policy |
|
Fair enough. You closed while I was in the process of updating the comment with the ping to seberg acknowledging the AI use. |
|
Ah ok, that changes things |
|
I'm still not sure how I feel about Claude as co-author though. |
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. |
|
Also sorry for the drive-by comment, I think improvements like this are great and I'll try to get it reviewed. |
seberg
left a comment
There was a problem hiding this comment.
Thanks, a few nits that would be nice to address. This tends to be a little bit more verbose then I like, but I tall looks good.
| return 0; | ||
| } | ||
| if (npy_fabs(result) >= 9223372036854775808.0 /* 2**63 */) { | ||
| return 1; |
There was a problem hiding this comment.
NIT: I'd prolly use -1 just because it's the more typical pattern, I think. Maybe we should also in-line the error, just to avoid duplicating the message?
| /* Division by exactly zero yields NaT, matching numpy's | ||
| * convention. Unlike a tiny nonzero divisor (whose infinite | ||
| * quotient is an overflow), this is not treated as overflow. */ | ||
| *((npy_timedelta *)op1) = NPY_DATETIME_NAT; |
There was a problem hiding this comment.
Not sure I understand the comment and path. If in2 is zero, the result will be NaN and timedelta_from_double will do just fine. Just delete it?
The old code had the extra branch to map infinities to NaN, but you want to change that to raise an error aggressively (debatable, but IIRC there were some previous PRs where this was effectively decided more generally for datetime).
| # NaN operand yields NaT, not overflow. | ||
| assert np.isnat(td * np.float64(np.nan)) | ||
| assert np.isnat(np.float64(np.nan) * td) | ||
| assert np.isnat(td / np.float64(np.nan)) |
There was a problem hiding this comment.
With these AI tests I always feel they should be parametrized more (which I would appreciate if easy), but OK...
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