-
-
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
Conversation
pandas/core/indexes/datetimes.py
Outdated
# raise instead of incorrectly wrapping around GH#19740 | ||
e = b + np.int64(periods) * stride | ||
except (FloatingPointError, OverflowError): | ||
raise libts.OutOfBoundsDatetime('Cannot generate range with ' |
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.
there is already a routine to do this checking (not called here) pls use it
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.
greo for overflow
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.
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.
grep the codebase (I put it below)
Codecov Report
@@ Coverage Diff @@
## master #19740 +/- ##
==========================================
+ Coverage 91.58% 91.6% +0.02%
==========================================
Files 150 150
Lines 48862 48873 +11
==========================================
+ Hits 44750 44770 +20
+ Misses 4112 4103 -9
Continue to review full report at Codecov.
|
|
OK. Do we care about catching OverflowError and re-raising as OutOfBoundsDatetime? If not then this is super-simple. If so then most of the code added here is still needed. |
i think we should reraise as oob |
Agreed. Done. |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might make sense to :
move this to datetimelike.py
remove the side arg (and just o on passing in)
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The side
arg is needed if we want the error message to be specific.
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
... you mean like the original implementation of the PR?
this closes #14187 pls add appropriate tests from that issue |
I'll add the GH reference to the test already implemented. That test covers the 14187's example almost exactly; only difference is the starting date passed to date_range. |
@@ -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 comment
The 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
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
... you mean like the original implementation of the PR?
well the original impl you had a badly written version of checked_add_with_arr
for 2 cases don't use a function, for more than 2 that would be ok.
@@ -716,7 +716,7 @@ Datetimelike | |||
- Bug in :class:`Timestamp` and :func:`to_datetime` where a string representing a barely out-of-bounds timestamp would be incorrectly rounded down instead of raising ``OutOfBoundsDatetime`` (:issue:`19382`) | |||
- Bug in :func:`Timestamp.floor` :func:`DatetimeIndex.floor` where time stamps far in the future and past were not rounded correctly (:issue:`19206`) | |||
- Bug in :func:`to_datetime` where passing an out-of-bounds datetime with ``errors='coerce'`` and ``utc=True`` would raise ``OutOfBoundsDatetime`` instead of parsing to ``NaT`` (:issue:`19612`) | |||
- | |||
- Bug in :func:`date_range` with frequency of ``Day`` or higher where dates sufficiently far in the future could wrap around to the past instead of raising ``OutOfBoundsDatetime`` (:issue:`19740`) |
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
I call BS. You're welcome to be demanding but I won't accept aspersions given that it was literally just wrapping the one line that is already there. Closing. |
git diff upstream/master -u -- "*.py" | flake8 --diff