Skip to content

Commit a3f6583

Browse files
committed
Add date overflow message to tz_localize (pandas-dev#32967)
1 parent c15f080 commit a3f6583

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ Datetimelike
897897
resolution which converted to object dtype instead of coercing to ``datetime64[ns]``
898898
dtype when within the timestamp bounds (:issue:`34843`).
899899
- 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`)
900+
- ``OutOfBoundsDatetime`` issues an error message when timestamp is out of implementation bounds. (:issue:`32967`)
900901

901902
Timedelta
902903
^^^^^^^^^

pandas/_libs/tslibs/conversion.pyx

+10-3
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,20 @@ cdef inline check_overflows(_TSObject obj):
605605
OutOfBoundsDatetime
606606
"""
607607
# GH#12677
608+
fmt = (f'{obj.dts.year}-{obj.dts.month:02d}-{obj.dts.day:02d} '
609+
f'{obj.dts.hour:02d}:{obj.dts.min:02d}:{obj.dts.sec:02d}')
608610
if obj.dts.year == 1677:
609611
if not (obj.value < 0):
610-
raise OutOfBoundsDatetime
612+
from pandas._libs.tslibs.timestamps import Timestamp
613+
raise OutOfBoundsDatetime(
614+
f'Converting {fmt} underflows past {Timestamp.min}'
615+
)
611616
elif obj.dts.year == 2262:
612617
if not (obj.value > 0):
613-
raise OutOfBoundsDatetime
614-
618+
from pandas._libs.tslibs.timestamps import Timestamp
619+
raise OutOfBoundsDatetime(
620+
f'Converting {fmt} overflows past {Timestamp.max}'
621+
)
615622

616623
# ----------------------------------------------------------------------
617624
# Localization

pandas/tests/scalar/timestamp/test_timezones.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@ class TestTimestampTZOperations:
2121
# Timestamp.tz_localize
2222

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

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

0 commit comments

Comments
 (0)