Skip to content

Commit f0b35f2

Browse files
eovesontm9k1
authored andcommitted
TST: Add test case for GH14080 for overflow exception (pandas-dev#23762)
1 parent 2b890f4 commit f0b35f2

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

pandas/tests/scalar/timestamp/test_arithmetic.py

+40-7
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,58 @@
77
import pandas.util.testing as tm
88
from pandas.compat import long
99
from pandas.tseries import offsets
10+
from pandas.tseries.frequencies import to_offset
1011
from pandas import Timestamp, Timedelta
1112

1213

1314
class TestTimestampArithmetic(object):
1415
def test_overflow_offset(self):
16+
# no overflow expected
17+
18+
stamp = Timestamp("2000/1/1")
19+
offset_no_overflow = to_offset("D") * 100
20+
21+
expected = Timestamp("2000/04/10")
22+
assert stamp + offset_no_overflow == expected
23+
24+
assert offset_no_overflow + stamp == expected
25+
26+
expected = Timestamp("1999/09/23")
27+
assert stamp - offset_no_overflow == expected
28+
29+
def test_overflow_offset_raises(self):
1530
# xref https://github.com/statsmodels/statsmodels/issues/3374
1631
# ends up multiplying really large numbers which overflow
1732

1833
stamp = Timestamp('2017-01-13 00:00:00', freq='D')
19-
offset = 20169940 * offsets.Day(1)
34+
offset_overflow = 20169940 * offsets.Day(1)
35+
msg = ("the add operation between "
36+
r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} "
37+
"will overflow")
38+
39+
with pytest.raises(OverflowError, match=msg):
40+
stamp + offset_overflow
41+
42+
with pytest.raises(OverflowError, match=msg):
43+
offset_overflow + stamp
44+
45+
with pytest.raises(OverflowError, match=msg):
46+
stamp - offset_overflow
47+
48+
# xref https://github.com/pandas-dev/pandas/issues/14080
49+
# used to crash, so check for proper overflow exception
50+
51+
stamp = Timestamp("2000/1/1")
52+
offset_overflow = to_offset("D") * 100 ** 25
2053

21-
with pytest.raises(OverflowError):
22-
stamp + offset
54+
with pytest.raises(OverflowError, match=msg):
55+
stamp + offset_overflow
2356

24-
with pytest.raises(OverflowError):
25-
offset + stamp
57+
with pytest.raises(OverflowError, match=msg):
58+
offset_overflow + stamp
2659

27-
with pytest.raises(OverflowError):
28-
stamp - offset
60+
with pytest.raises(OverflowError, match=msg):
61+
stamp - offset_overflow
2962

3063
def test_delta_preserve_nanos(self):
3164
val = Timestamp(long(1337299200000000123))

0 commit comments

Comments
 (0)