-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
fix overflows in Timestamp.tz_localize near boundaries #19626
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
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -309,12 +309,13 @@ cdef convert_to_tsobject(object ts, object tz, object unit, | |
raise TypeError('Cannot convert input [{}] of type {} to ' | ||
'Timestamp'.format(ts, type(ts))) | ||
|
||
if obj.value != NPY_NAT: | ||
check_dts_bounds(&obj.dts) | ||
|
||
if tz is not None: | ||
_localize_tso(obj, tz) | ||
localize_tso(obj, tz) | ||
|
||
if obj.value != NPY_NAT: | ||
# check_silent_overflows needs to run after localize_tso | ||
check_dts_bounds(&obj.dts) | ||
check_silent_overflows(obj) | ||
return obj | ||
|
||
|
||
|
@@ -391,6 +392,7 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, object tz, | |
obj.dts.ps = nanos * 1000 | ||
|
||
check_dts_bounds(&obj.dts) | ||
check_silent_overflows(obj) | ||
return obj | ||
|
||
|
||
|
@@ -454,6 +456,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit, | |
obj.value = tz_convert_single(obj.value, obj.tzinfo, 'UTC') | ||
if tz is None: | ||
check_dts_bounds(&obj.dts) | ||
check_silent_overflows(obj) | ||
return obj | ||
else: | ||
# Keep the converter same as PyDateTime's | ||
|
@@ -469,7 +472,7 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit, | |
else: | ||
ts = obj.value | ||
if tz is not None: | ||
# shift for _localize_tso | ||
# shift for localize_tso | ||
ts = tz_localize_to_utc(np.array([ts], dtype='i8'), tz, | ||
ambiguous='raise', | ||
errors='raise')[0] | ||
|
@@ -490,12 +493,37 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit, | |
return convert_to_tsobject(ts, tz, unit, dayfirst, yearfirst) | ||
|
||
|
||
cdef inline check_silent_overflows(_TSObject obj): | ||
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. rename, add doc-string (just convert your comment) |
||
# Check that we haven't silently overflowed in timezone conversion | ||
# GH#12677 | ||
if obj.dts.year == 1677: | ||
if not (obj.value < 0): | ||
raise OutOfBoundsDatetime | ||
elif obj.dts.year == 2262: | ||
if not (obj.value > 0): | ||
raise OutOfBoundsDatetime | ||
|
||
|
||
# ---------------------------------------------------------------------- | ||
# Localization | ||
|
||
cdef inline void _localize_tso(_TSObject obj, object tz): | ||
cdef inline void localize_tso(_TSObject obj, tzinfo tz): | ||
""" | ||
Take a TSObject in UTC and localizes to timezone tz. | ||
Given the UTC nanosecond timestamp in obj.value, find the wall-clock | ||
representation of that timestamp in the given timezone. | ||
|
||
Parameters | ||
---------- | ||
obj : _TSObject | ||
tz : tzinfo | ||
|
||
Returns | ||
------- | ||
(void) | ||
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. None |
||
|
||
Notes | ||
----- | ||
Sets obj.tzinfo inplace, alters obj.dts inplace. | ||
""" | ||
cdef: | ||
ndarray[int64_t] trans, deltas | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rename to check_overlows