From 5349052667163ea2baa8897b050950c9215bf434 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sat, 17 Nov 2018 20:17:23 -0800 Subject: [PATCH 1/4] TST: Add test case for GH14080 for overflow exception --- .../tests/scalar/timestamp/test_arithmetic.py | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index 0f8ddc53734f6..6120d024cfd69 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -7,11 +7,12 @@ 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): + def test_overflow_offset1(self): # xref https://github.com/statsmodels/statsmodels/issues/3374 # ends up multiplying really large numbers which overflow @@ -27,6 +28,30 @@ def test_overflow_offset(self): with pytest.raises(OverflowError): stamp - offset + def test_overflow_offset2(self): + # xref https://github.com/pandas-dev/pandas/issues/14080 + + stamp = Timestamp("2000/1/1") + offset_overflow = to_offset("D")*100**25 + offset_no_overflow = to_offset("D")*100 + + with pytest.raises(OverflowError): + stamp + offset_overflow + + with pytest.raises(OverflowError): + offset_overflow + stamp + + with pytest.raises(OverflowError): + stamp - offset_overflow + + 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_delta_preserve_nanos(self): val = Timestamp(long(1337299200000000123)) result = val + timedelta(1) From a7df6eade76f0f190332696ae1d9871fd17389aa Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sat, 17 Nov 2018 20:39:37 -0800 Subject: [PATCH 2/4] TST: Add test case for GH14080 for overflow exception --- pandas/tests/scalar/timestamp/test_arithmetic.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index 6120d024cfd69..e8f4d93613f76 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -30,11 +30,13 @@ def test_overflow_offset1(self): def test_overflow_offset2(self): # 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 offset_no_overflow = to_offset("D")*100 + # overflow expected with pytest.raises(OverflowError): stamp + offset_overflow @@ -44,6 +46,7 @@ def test_overflow_offset2(self): with pytest.raises(OverflowError): stamp - offset_overflow + # no overflow expected expected = Timestamp("2000/04/10") assert stamp + offset_no_overflow == expected From f7be8f3828f9eddd204af577216e388d7cf5da12 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sat, 17 Nov 2018 20:53:20 -0800 Subject: [PATCH 3/4] TST: Add test case for GH14080 for overflow exception --- pandas/tests/scalar/timestamp/test_arithmetic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index e8f4d93613f76..a79b39b641064 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -33,8 +33,8 @@ def test_overflow_offset2(self): # used to crash, so check for proper overflow exception stamp = Timestamp("2000/1/1") - offset_overflow = to_offset("D")*100**25 - offset_no_overflow = to_offset("D")*100 + offset_overflow = to_offset("D") * 100 ** 25 + offset_no_overflow = to_offset("D") * 100 # overflow expected with pytest.raises(OverflowError): From c727003fd4f9619dffe3c555de8cce3158497919 Mon Sep 17 00:00:00 2001 From: Erik Oveson Date: Sun, 18 Nov 2018 08:45:33 -0800 Subject: [PATCH 4/4] TST: For GH14080, break up tests, test message --- .../tests/scalar/timestamp/test_arithmetic.py | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_arithmetic.py b/pandas/tests/scalar/timestamp/test_arithmetic.py index a79b39b641064..207bd103105ea 100644 --- a/pandas/tests/scalar/timestamp/test_arithmetic.py +++ b/pandas/tests/scalar/timestamp/test_arithmetic.py @@ -12,49 +12,54 @@ class TestTimestampArithmetic(object): - def test_overflow_offset1(self): + 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): - 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_overflow_offset2(self): # 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 - offset_no_overflow = to_offset("D") * 100 - # overflow expected - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): stamp + offset_overflow - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): offset_overflow + stamp - with pytest.raises(OverflowError): + with pytest.raises(OverflowError, match=msg): stamp - offset_overflow - # no overflow expected - 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_delta_preserve_nanos(self): val = Timestamp(long(1337299200000000123)) result = val + timedelta(1)