Skip to content

Commit 992ca95

Browse files
finer-grained testing for td64 sum overflow errors
1 parent 287ca88 commit 992ca95

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

pandas/tests/series/test_reductions.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from hypothesis import given
1+
from hypothesis import given, note
22
import hypothesis.strategies as st
33
import numpy as np
44
import pytest
@@ -95,29 +95,27 @@ def test_td64_summation_raises_spurious_overflow_error_for_single_elem_series_wi
9595
pd.Series(value).sum()
9696

9797

98-
def test_td64_summation_overflow():
99-
# GH#9442
100-
ser = Series(pd.date_range("20130101", periods=100000, freq="H"))
101-
ser[0] += pd.Timedelta("1s 1ms")
98+
@given(st.integers(min_value=1, max_value=2**10).map(pd.Timedelta))
99+
def test_td64_summation_raises_overflow_error_for_small_overflows(value: pd.Timedelta):
100+
s = pd.Series([pd.Timedelta.max, value])
102101

103-
# mean
104-
result = (ser - ser.min()).mean()
105-
expected = pd.Timedelta((pd.TimedeltaIndex(ser - ser.min()).asi8 / len(ser)).sum())
102+
msg = "Python int too large to convert to C long"
103+
with pytest.raises(OverflowError, match=msg):
104+
s.sum()
106105

107-
# the computation is converted to float so
108-
# might be some loss of precision
109-
assert np.allclose(result.value / 1000, expected.value / 1000)
110106

111-
# sum
112-
msg = "overflow in timedelta operation"
113-
with pytest.raises(ValueError, match=msg):
114-
(ser - ser.min()).sum()
107+
@given(
108+
st.integers(
109+
min_value=2**10 + 1,
110+
max_value=pd.Timedelta.max.value,
111+
).map(pd.Timedelta)
112+
)
113+
def test_td64_summation_raises_value_error_for_most_overflows(value: pd.Timedelta):
114+
s = pd.Series([pd.Timedelta.max, value])
115115

116-
s1 = ser[0:10000]
116+
msg = "overflow in timedelta operation"
117117
with pytest.raises(ValueError, match=msg):
118-
(s1 - s1.min()).sum()
119-
s2 = ser[0:1000]
120-
(s2 - s2.min()).sum()
118+
s.sum()
121119

122120

123121
def test_prod_numpy16_bug():

0 commit comments

Comments
 (0)