Skip to content

Add date overflow message to tz_localize (#32967) #35187

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 2 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ Datetimelike
resolution which converted to object dtype instead of coercing to ``datetime64[ns]``
dtype when within the timestamp bounds (:issue:`34843`).
- The ``freq`` keyword in :class:`Period`, :func:`date_range`, :func:`period_range`, :func:`pd.tseries.frequencies.to_offset` no longer allows tuples, pass as string instead (:issue:`34703`)
- ``OutOfBoundsDatetime`` issues an error message when timestamp is out of implementation bounds. (:issue:`32967`)

Timedelta
^^^^^^^^^
Expand Down
13 changes: 10 additions & 3 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,20 @@ cdef inline check_overflows(_TSObject obj):
OutOfBoundsDatetime
"""
# GH#12677
fmt = (f'{obj.dts.year}-{obj.dts.month:02d}-{obj.dts.day:02d} '
f'{obj.dts.hour:02d}:{obj.dts.min:02d}:{obj.dts.sec:02d}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you delay evaluating this until we know we're raising?

also pls use double-quotes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use a lambda x: ... here and use .format(obj)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we can milk a tiny bit more performance by avoiding defining the lambda

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, you end up having to actually either define everything twice (gross) or use a function call, which is precisely what i am suggesting

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define everything twice (gross) or use a function call

Even if you're not evaluating the lambda, defining it at runtime is overhead we can avoid.

i expect the performance difference is tiny, but fstrings showed up in a perf regression @TomAugspurger fixed the other day.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just went the defining twice route for now, but I agree it kinda feels gross. Let me know if I should change it.

if obj.dts.year == 1677:
if not (obj.value < 0):
raise OutOfBoundsDatetime
from pandas._libs.tslibs.timestamps import Timestamp
raise OutOfBoundsDatetime(
f'Converting {fmt} underflows past {Timestamp.min}'
)
elif obj.dts.year == 2262:
if not (obj.value > 0):
raise OutOfBoundsDatetime

from pandas._libs.tslibs.timestamps import Timestamp
raise OutOfBoundsDatetime(
f'Converting {fmt} overflows past {Timestamp.max}'
)

# ----------------------------------------------------------------------
# Localization
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/scalar/timestamp/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ class TestTimestampTZOperations:
# Timestamp.tz_localize

def test_tz_localize_pushes_out_of_bounds(self):
msg = "^$"
# GH#12677
# tz_localize that pushes away from the boundary is OK
msg = (
f"Converting {Timestamp.min.strftime('%Y-%m-%d %H:%M:%S')} "
f"underflows past {Timestamp.min}"
)
pac = Timestamp.min.tz_localize("US/Pacific")
assert pac.value > Timestamp.min.value
pac.tz_convert("Asia/Tokyo") # tz_convert doesn't change value
with pytest.raises(OutOfBoundsDatetime, match=msg):
Timestamp.min.tz_localize("Asia/Tokyo")

# tz_localize that pushes away from the boundary is OK
msg = (
f"Converting {Timestamp.max.strftime('%Y-%m-%d %H:%M:%S')} "
f"overflows past {Timestamp.max}"
)
tokyo = Timestamp.max.tz_localize("Asia/Tokyo")
assert tokyo.value < Timestamp.max.value
tokyo.tz_convert("US/Pacific") # tz_convert doesn't change value
Expand Down