Skip to content

Commit 8d11ac1

Browse files
gabrielNTfangchenli
authored andcommitted
Add date overflow message to tz_localize (pandas-dev#32967) (pandas-dev#35187)
* Add date overflow message to tz_localize (pandas-dev#32967) * Delay evaluating timestamp
1 parent ab1e279 commit 8d11ac1

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

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

926927
Timedelta
927928
^^^^^^^^^

pandas/_libs/tslibs/conversion.pyx

+12-3
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,20 @@ cdef inline check_overflows(_TSObject obj):
639639
# GH#12677
640640
if obj.dts.year == 1677:
641641
if not (obj.value < 0):
642-
raise OutOfBoundsDatetime
642+
from pandas._libs.tslibs.timestamps import Timestamp
643+
fmt = (f"{obj.dts.year}-{obj.dts.month:02d}-{obj.dts.day:02d} "
644+
f"{obj.dts.hour:02d}:{obj.dts.min:02d}:{obj.dts.sec:02d}")
645+
raise OutOfBoundsDatetime(
646+
f"Converting {fmt} underflows past {Timestamp.min}"
647+
)
643648
elif obj.dts.year == 2262:
644649
if not (obj.value > 0):
645-
raise OutOfBoundsDatetime
646-
650+
from pandas._libs.tslibs.timestamps import Timestamp
651+
fmt = (f"{obj.dts.year}-{obj.dts.month:02d}-{obj.dts.day:02d} "
652+
f"{obj.dts.hour:02d}:{obj.dts.min:02d}:{obj.dts.sec:02d}")
653+
raise OutOfBoundsDatetime(
654+
f"Converting {fmt} overflows past {Timestamp.max}"
655+
)
647656

648657
# ----------------------------------------------------------------------
649658
# 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)