Skip to content

Better error message for OOB result #32499

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 6 commits into from
Mar 11, 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.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Bug fixes

- Bug in :meth:`DataFrame.reindex` and :meth:`Series.reindex` when reindexing with a tz-aware index (:issue:`26683`)
- Bug where :func:`to_datetime` would raise when passed ``pd.NA`` (:issue:`32213`)
- Improved error message when subtracting two :class:`Timestamp` that result in an invalide :class:`Timedelta` (:issue:`31774`)

**Categorical**

Expand Down
8 changes: 7 additions & 1 deletion pandas/_libs/tslibs/c_timestamp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,15 @@ cdef class _Timestamp(datetime):
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
# Timedelta
from pandas._libs.tslibs.timedeltas import Timedelta
from pandas._libs.tslibs.timestamps import Timestamp
try:
return Timedelta(self.value - other.value)
except (OverflowError, OutOfBoundsDatetime):
except (OverflowError, OutOfBoundsDatetime) as e:
if isinstance(other, Timestamp):
raise OverflowError(
"Result is too large for pandas.Timestamp. Convert inputs "
"to datetime.datetime before subtracting."
) from e
pass
elif is_datetime64_object(self):
# GH#28286 cython semantics for __rsub__, `other` is actually
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/scalar/timestamp/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ def test_overflow_offset_raises(self):
with pytest.raises(OverflowError, match=msg):
stamp - offset_overflow

def test_overflow_timestamp_raises(self):
# https://github.com/pandas-dev/pandas/issues/31774
msg = "Result is too large"
a = Timestamp("2101-01-01 00:00:00")
b = Timestamp("1688-01-01 00:00:00")

with pytest.raises(OverflowError, match=msg):
a - b

def test_delta_preserve_nanos(self):
val = Timestamp(1337299200000000123)
result = val + timedelta(1)
Expand Down