File tree Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,7 @@ Bug fixes
64
64
65
65
- Bug in :meth: `DataFrame.reindex ` and :meth: `Series.reindex ` when reindexing with a tz-aware index (:issue: `26683 `)
66
66
- Bug where :func: `to_datetime ` would raise when passed ``pd.NA `` (:issue: `32213 `)
67
+ - Improved error message when subtracting two :class: `Timestamp ` that result in an invalide :class: `Timedelta ` (:issue: `31774 `)
67
68
68
69
**Categorical **
69
70
Original file line number Diff line number Diff line change @@ -299,9 +299,15 @@ cdef class _Timestamp(datetime):
299
299
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
300
300
# Timedelta
301
301
from pandas._libs.tslibs.timedeltas import Timedelta
302
+ from pandas._libs.tslibs.timestamps import Timestamp
302
303
try :
303
304
return Timedelta(self .value - other.value)
304
- except (OverflowError , OutOfBoundsDatetime):
305
+ except (OverflowError , OutOfBoundsDatetime) as e:
306
+ if isinstance (other, Timestamp):
307
+ raise OverflowError (
308
+ " Result is too large for pandas.Timestamp. Convert inputs "
309
+ " to datetime.datetime before subtracting."
310
+ ) from e
305
311
pass
306
312
elif is_datetime64_object(self ):
307
313
# GH#28286 cython semantics for __rsub__, `other` is actually
Original file line number Diff line number Diff line change @@ -60,6 +60,15 @@ def test_overflow_offset_raises(self):
60
60
with pytest .raises (OverflowError , match = msg ):
61
61
stamp - offset_overflow
62
62
63
+ def test_overflow_timestamp_raises (self ):
64
+ # https://github.com/pandas-dev/pandas/issues/31774
65
+ msg = "Result is too large"
66
+ a = Timestamp ("2101-01-01 00:00:00" )
67
+ b = Timestamp ("1688-01-01 00:00:00" )
68
+
69
+ with pytest .raises (OverflowError , match = msg ):
70
+ a - b
71
+
63
72
def test_delta_preserve_nanos (self ):
64
73
val = Timestamp (1337299200000000123 )
65
74
result = val + timedelta (1 )
You can’t perform that action at this time.
0 commit comments