10000 BUG: Fix date_range overflow by jbrockmendel · Pull Request #23345 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix date_range overflow #23345

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

Merged
merged 9 commits into from
Oct 28, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wrap checked_add_with_arr
  • Loading branch information
jbrockmendel committed Feb 18, 2018
commit 54bd77598b88923e35c4eefc4214d404352eb942
61 changes: 40 additions & 21 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2137,30 +2137,11 @@ def _generate_regular_range(start, end, periods, offset):
tz = start.tz
elif start is not None:
b = Timestamp(start).value
try:
with np.errstate(over='raise'):
# raise instead of incorrectly wrapping around GH#19740
e = b + np.int64(periods) * stride
except (FloatingPointError, OverflowError):
raise libts.OutOfBoundsDatetime('Cannot generate range with '
'start={start} and '
'periods={periods}'
.format(start=start,
periods=periods))

e = _reraise_overflow_as_oob(b, periods, stride, side='start')
tz = start.tz
elif end is not None:
e = Timestamp(end).value + stride
try:
with np.errstate(over='raise'):
# raise instead of incorrectly wrapping around GH#19740
b = e - np.int64(periods) * stride
except (FloatingPointError, OverflowError):
raise libts.OutOfBoundsDatetime('Cannot generate range with '
'start={start} and '
'periods={periods}'
.format(start=start,
periods=periods))
b = _reraise_overflow_as_oob(e, periods, stride, side='end')
tz = end.tz
else:
raise ValueError("at least 'start' or 'end' should be specified "
Expand All @@ -2185,6 +2166,44 @@ def _generate_regular_range(start, end, periods, offset):
return data


def _reraise_overflow_as_oob(endpoint, periods, stride, side='start'):
"""
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):
"""
Expand Down
0