-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Fix wraparound/overflow in date_range #19740
Changes from all commits
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 |
---|---|---|
|
@@ -2137,11 +2137,11 @@ def _generate_regular_range(start, end, periods, offset): | |
tz = start.tz | ||
elif start is not None: | ||
b = Timestamp(start).value | ||
e = b + np.int64(periods) * stride | ||
e = _reraise_overflow_as_oob(b, periods, stride, side='start') | ||
tz = start.tz | ||
elif end is not None: | ||
e = Timestamp(end).value + stride | ||
b = e - np.int64(periods) * stride | ||
b = _reraise_overflow_as_oob(e, periods, stride, side='end') | ||
tz = end.tz | ||
else: | ||
raise ValueError("at least 'start' or 'end' should be specified " | ||
|
@@ -2166,6 +2166,44 @@ def _generate_regular_range(start, end, periods, offset): | |
return data | ||
|
||
|
||
def _reraise_overflow_as_oob(endpoint, periods, stride, side='start'): | ||
""" | ||
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 it might make sense to : the reason is we have several other uses of checked_add_with_arr which are catching a scalar add overflow and should be quite similar to this (the other is in Timedelta). the other uses I saw were for array-like input and they don't raise. 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. The All non-test usages of checked_add_with_arr in datetimelike/datetimes/timedeltas. We may be able to consolidate them in datetimelike. (though im pretty sure that numeric classes should have overflow checking implemented at some point) 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. my point is you are adding a function here, where if there are only 2 uses, its shorter / simpler just to use a try/except directly 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. my point is you are adding a function here, where if there are only 2 uses, its shorter / simpler just to use a try/except directly 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. ... you mean like the original implementation of the PR? |
||
Calculate the second endpoint for passing to np.arange, checking | ||
to avoid an integer overflow. Catch OverflowError and re-raise | ||
as OutOfBoundsDatetime. | ||
|
||
Parameters | ||
---------- | ||
endpoint : int | ||
periods : int | ||
stride : int | ||
side : {'start', 'end'} | ||
|
||
Returns | ||
------- | ||
other_end : int | ||
|
||
Raises | ||
------ | ||
OutOfBoundsDatetime | ||
""" | ||
# GH#19740 raise instead of incorrectly wrapping around | ||
assert side in ['start', 'end'] | ||
if side == 'end': | ||
stride *= -1 | ||
|
||
try: | ||
other_end = checked_add_with_arr(np.int64(endpoint), | ||
np.int64(periods) * stride) | ||
except OverflowError: | ||
raise libts.OutOfBoundsDatetime('Cannot generate range with ' | ||
'{side}={endpoint} and ' | ||
'periods={periods}' | ||
.format(side=side, endpoint=endpoint, | ||
periods=periods)) | ||
return other_end | ||
|
||
|
||
def date_range(start=None, end=None, periods=None, freq='D', tz=None, | ||
normalize=False, name=None, closed=None, **kwargs): | ||
""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the issue number here as well