|
7 | 7 | import pandas.util.testing as tm
|
8 | 8 | from pandas.compat import long
|
9 | 9 | from pandas.tseries import offsets
|
| 10 | +from pandas.tseries.frequencies import to_offset |
10 | 11 | from pandas import Timestamp, Timedelta
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | class TestTimestampArithmetic(object):
|
14 | 15 | 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): |
15 | 30 | # xref https://github.com/statsmodels/statsmodels/issues/3374
|
16 | 31 | # ends up multiplying really large numbers which overflow
|
17 | 32 |
|
18 | 33 | 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 |
20 | 53 |
|
21 |
| - with pytest.raises(OverflowError): |
22 |
| - stamp + offset |
| 54 | + with pytest.raises(OverflowError, match=msg): |
| 55 | + stamp + offset_overflow |
23 | 56 |
|
24 |
| - with pytest.raises(OverflowError): |
25 |
| - offset + stamp |
| 57 | + with pytest.raises(OverflowError, match=msg): |
| 58 | + offset_overflow + stamp |
26 | 59 |
|
27 |
| - with pytest.raises(OverflowError): |
28 |
| - stamp - offset |
| 60 | + with pytest.raises(OverflowError, match=msg): |
| 61 | + stamp - offset_overflow |
29 | 62 |
|
30 | 63 | def test_delta_preserve_nanos(self):
|
31 | 64 | val = Timestamp(long(1337299200000000123))
|
|
0 commit comments