Skip to content

Commit

Permalink
TST: Add test case for GH14080 for overflow exception (pandas-dev#23762
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eoveson authored and tm9k1 committed Nov 19, 2018
1 parent 2b890f4 commit f0b35f2
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions pandas/tests/scalar/timestamp/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,58 @@
import pandas.util.testing as tm
from pandas.compat import long
from pandas.tseries import offsets
from pandas.tseries.frequencies import to_offset
from pandas import Timestamp, Timedelta


class TestTimestampArithmetic(object):
def test_overflow_offset(self):
# no overflow expected

stamp = Timestamp("2000/1/1")
offset_no_overflow = to_offset("D") * 100

expected = Timestamp("2000/04/10")
assert stamp + offset_no_overflow == expected

assert offset_no_overflow + stamp == expected

expected = Timestamp("1999/09/23")
assert stamp - offset_no_overflow == expected

def test_overflow_offset_raises(self):
# xref https://github.com/statsmodels/statsmodels/issues/3374
# ends up multiplying really large numbers which overflow

stamp = Timestamp('2017-01-13 00:00:00', freq='D')
offset = 20169940 * offsets.Day(1)
offset_overflow = 20169940 * offsets.Day(1)
msg = ("the add operation between "
r"\<-?\d+ \* Days\> and \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} "
"will overflow")

with pytest.raises(OverflowError, match=msg):
stamp + offset_overflow

with pytest.raises(OverflowError, match=msg):
offset_overflow + stamp

with pytest.raises(OverflowError, match=msg):
stamp - offset_overflow

# xref https://github.com/pandas-dev/pandas/issues/14080
# used to crash, so check for proper overflow exception

stamp = Timestamp("2000/1/1")
offset_overflow = to_offset("D") * 100 ** 25

with pytest.raises(OverflowError):
stamp + offset
with pytest.raises(OverflowError, match=msg):
stamp + offset_overflow

with pytest.raises(OverflowError):
offset + stamp
with pytest.raises(OverflowError, match=msg):
offset_overflow + stamp

with pytest.raises(OverflowError):
stamp - offset
with pytest.raises(OverflowError, match=msg):
stamp - offset_overflow

def test_delta_preserve_nanos(self):
val = Timestamp(long(1337299200000000123))
Expand Down

0 comments on commit f0b35f2

Please sign in to comment.