-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
BUG: ensure_timedelta64ns overflows #34448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
005f5b4
f607a1a
3356d90
ef4c651
a73f9c2
6bfb246
6631080
34fbc53
409e7e6
0f95f73
e87b5a2
b89ae3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,11 +217,35 @@ def ensure_timedelta64ns(arr: ndarray, copy: bool=True): | |
|
||
Returns | ||
------- | ||
result : ndarray with dtype timedelta64[ns] | ||
|
||
ndarray[timedelta64[ns]] | ||
""" | ||
return arr.astype(TD64NS_DTYPE, copy=copy) | ||
# TODO: check for overflows when going from a lower-resolution to nanos | ||
assert arr.dtype.kind == "m", arr.dtype | ||
|
||
if arr.dtype == TD64NS_DTYPE: | ||
return arr.copy() if copy else arr | ||
|
||
# Re-use the datetime64 machinery to do an overflow-safe `astype` | ||
dtype = arr.dtype.str.replace("m8", "M8") | ||
dummy = arr.view(dtype) | ||
try: | ||
dt64_result = ensure_datetime64ns(dummy, copy) | ||
except OutOfBoundsDatetime as err: | ||
# Re-write the exception in terms of timedelta64 instead of dt64 | ||
|
||
# Find the value that we are going to report as causing an overflow | ||
tdmin = arr.min() | ||
tdmax = arr.max() | ||
if np.abs(tdmin) >= np.abs(tdmax): | ||
bad_val = tdmin | ||
else: | ||
bad_val = tdmax | ||
|
||
unit_str = arr.dtype.str.split("[")[-1][:-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems fragile, can you just report it entirely:
|
||
raise OutOfBoundsDatetime( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we just create OutOfBoundsTimedelta There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think if we create an OOBTimedelta exception would be ok ith this change |
||
f"Out of bounds for nanosecond timedelta {bad_val}[{unit_str}]" | ||
) | ||
|
||
return dt64_result.view(TD64NS_DTYPE) | ||
|
||
|
||
# ---------------------------------------------------------------------- | ||
|
Uh oh!
There was an error while loading. Please reload this page.