Skip to content

Commit 44adb8f

Browse files
authored
ERR: re-raise OverflowError as OutOfBoundsTimedelta (#40065)
1 parent fc4d3f2 commit 44adb8f

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

pandas/_libs/tslibs/timedeltas.pyx

+10-6
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ cpdef int64_t delta_to_nanoseconds(delta) except? -1:
178178
if is_integer_object(delta):
179179
return delta
180180
if PyDelta_Check(delta):
181-
return (
182-
delta.days * 24 * 60 * 60 * 1_000_000
183-
+ delta.seconds * 1_000_000
184-
+ delta.microseconds
185-
) * 1000
181+
try:
182+
return (
183+
delta.days * 24 * 60 * 60 * 1_000_000
184+
+ delta.seconds * 1_000_000
185+
+ delta.microseconds
186+
) * 1000
187+
except OverflowError as err:
188+
from pandas._libs.tslibs.conversion import OutOfBoundsTimedelta
189+
raise OutOfBoundsTimedelta(*err.args) from err
186190

187191
raise TypeError(type(delta))
188192

@@ -246,7 +250,7 @@ cdef object ensure_td64ns(object ts):
246250
td64_value = td64_value * mult
247251
except OverflowError as err:
248252
from pandas._libs.tslibs.conversion import OutOfBoundsTimedelta
249-
raise OutOfBoundsTimedelta(ts)
253+
raise OutOfBoundsTimedelta(ts) from err
250254

251255
return np.timedelta64(td64_value, "ns")
252256

pandas/tests/scalar/timedelta/test_arithmetic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
from pandas.compat import is_numpy_dev
14+
from pandas.errors import OutOfBoundsTimedelta
1415

1516
import pandas as pd
1617
from pandas import (
@@ -104,7 +105,7 @@ def test_td_add_timestamp_overflow(self):
104105
with pytest.raises(OverflowError, match=msg):
105106
Timestamp("1700-01-01") + Timedelta(13 * 19999, unit="D")
106107

107-
with pytest.raises(OverflowError, match=msg):
108+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
108109
Timestamp("1700-01-01") + timedelta(days=13 * 19999)
109110

110111
@pytest.mark.parametrize("op", [operator.add, ops.radd])

pandas/tests/scalar/timedelta/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_overflow_on_construction():
200200
with pytest.raises(OverflowError, match=msg):
201201
Timedelta(7 * 19999, unit="D")
202202

203-
with pytest.raises(OverflowError, match=msg):
203+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
204204
Timedelta(timedelta(days=13 * 19999))
205205

206206

pandas/tests/tools/test_to_datetime.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
iNaT,
2121
parsing,
2222
)
23-
from pandas.errors import OutOfBoundsDatetime
23+
from pandas.errors import (
24+
OutOfBoundsDatetime,
25+
OutOfBoundsTimedelta,
26+
)
2427
import pandas.util._test_decorators as td
2528

2629
from pandas.core.dtypes.common import is_datetime64_ns_dtype
@@ -1675,12 +1678,14 @@ def test_to_datetime_overflow(self):
16751678
# gh-17637
16761679
# we are overflowing Timedelta range here
16771680

1678-
msg = (
1679-
"(Python int too large to convert to C long)|"
1680-
"(long too big to convert)|"
1681-
"(int too big to convert)"
1681+
msg = "|".join(
1682+
[
1683+
"Python int too large to convert to C long",
1684+
"long too big to convert",
1685+
"int too big to convert",
1686+
]
16821687
)
1683-
with pytest.raises(OverflowError, match=msg):
1688+
with pytest.raises(OutOfBoundsTimedelta, match=msg):
16841689
date_range(start="1/1/1700", freq="B", periods=100000)
16851690

16861691
@pytest.mark.parametrize("cache", [True, False])

0 commit comments

Comments
 (0)