Skip to content

Commit 65e5912

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF: collect reduction tests (pandas-dev#36901)
1 parent c32dfaa commit 65e5912

File tree

4 files changed

+89
-81
lines changed

4 files changed

+89
-81
lines changed

pandas/tests/series/test_analytics.py

-77
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99

1010

1111
class TestSeriesAnalytics:
12-
def test_prod_numpy16_bug(self):
13-
s = Series([1.0, 1.0, 1.0], index=range(3))
14-
result = s.prod()
15-
16-
assert not isinstance(result, Series)
17-
1812
def test_matmul(self):
1913
# matmul test is for GH #10259
2014
a = Series(np.random.randn(4), index=["p", "q", "r", "s"])
@@ -125,74 +119,3 @@ def test_is_monotonic(self):
125119
s = Series(list(reversed(s.tolist())))
126120
assert s.is_monotonic is False
127121
assert s.is_monotonic_decreasing is True
128-
129-
@pytest.mark.parametrize("func", [np.any, np.all])
130-
@pytest.mark.parametrize("kwargs", [dict(keepdims=True), dict(out=object())])
131-
def test_validate_any_all_out_keepdims_raises(self, kwargs, func):
132-
s = pd.Series([1, 2])
133-
param = list(kwargs)[0]
134-
name = func.__name__
135-
136-
msg = (
137-
f"the '{param}' parameter is not "
138-
"supported in the pandas "
139-
fr"implementation of {name}\(\)"
140-
)
141-
with pytest.raises(ValueError, match=msg):
142-
func(s, **kwargs)
143-
144-
def test_validate_sum_initial(self):
145-
s = pd.Series([1, 2])
146-
msg = (
147-
r"the 'initial' parameter is not "
148-
r"supported in the pandas "
149-
r"implementation of sum\(\)"
150-
)
151-
with pytest.raises(ValueError, match=msg):
152-
np.sum(s, initial=10)
153-
154-
def test_validate_median_initial(self):
155-
s = pd.Series([1, 2])
156-
msg = (
157-
r"the 'overwrite_input' parameter is not "
158-
r"supported in the pandas "
159-
r"implementation of median\(\)"
160-
)
161-
with pytest.raises(ValueError, match=msg):
162-
# It seems like np.median doesn't dispatch, so we use the
163-
# method instead of the ufunc.
164-
s.median(overwrite_input=True)
165-
166-
def test_validate_stat_keepdims(self):
167-
s = pd.Series([1, 2])
168-
msg = (
169-
r"the 'keepdims' parameter is not "
170-
r"supported in the pandas "
171-
r"implementation of sum\(\)"
172-
)
173-
with pytest.raises(ValueError, match=msg):
174-
np.sum(s, keepdims=True)
175-
176-
def test_td64_summation_overflow(self):
177-
# GH 9442
178-
s = pd.Series(pd.date_range("20130101", periods=100000, freq="H"))
179-
s[0] += pd.Timedelta("1s 1ms")
180-
181-
# mean
182-
result = (s - s.min()).mean()
183-
expected = pd.Timedelta((pd.TimedeltaIndex(s - s.min()).asi8 / len(s)).sum())
184-
185-
# the computation is converted to float so
186-
# might be some loss of precision
187-
assert np.allclose(result.value / 1000, expected.value / 1000)
188-
189-
# sum
190-
msg = "overflow in timedelta operation"
191-
with pytest.raises(ValueError, match=msg):
192-
(s - s.min()).sum()
193-
194-
s1 = s[0:10000]
195-
with pytest.raises(ValueError, match=msg):
196-
(s1 - s1.min()).sum()
197-
s2 = s[0:1000]
198-
(s2 - s2.min()).sum()

pandas/tests/series/test_reductions.py

+86
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import numpy as np
2+
import pytest
3+
14
import pandas as pd
25
from pandas import Series
36

@@ -9,3 +12,86 @@ def test_reductions_td64_with_nat():
912
assert ser.median() == exp
1013
assert ser.min() == exp
1114
assert ser.max() == exp
15+
16+
17+
def test_td64_summation_overflow():
18+
# GH#9442
19+
ser = Series(pd.date_range("20130101", periods=100000, freq="H"))
20+
ser[0] += pd.Timedelta("1s 1ms")
21+
22+
# mean
23+
result = (ser - ser.min()).mean()
24+
expected = pd.Timedelta((pd.TimedeltaIndex(ser - ser.min()).asi8 / len(ser)).sum())
25+
26+
# the computation is converted to float so
27+
# might be some loss of precision
28+
assert np.allclose(result.value / 1000, expected.value / 1000)
29+
30+
# sum
31+
msg = "overflow in timedelta operation"
32+
with pytest.raises(ValueError, match=msg):
33+
(ser - ser.min()).sum()
34+
35+
s1 = ser[0:10000]
36+
with pytest.raises(ValueError, match=msg):
37+
(s1 - s1.min()).sum()
38+
s2 = ser[0:1000]
39+
(s2 - s2.min()).sum()
40+
41+
42+
def test_prod_numpy16_bug():
43+
ser = Series([1.0, 1.0, 1.0], index=range(3))
44+
result = ser.prod()
45+
46+
assert not isinstance(result, Series)
47+
48+
49+
@pytest.mark.parametrize("func", [np.any, np.all])
50+
@pytest.mark.parametrize("kwargs", [dict(keepdims=True), dict(out=object())])
51+
def test_validate_any_all_out_keepdims_raises(kwargs, func):
52+
ser = Series([1, 2])
53+
param = list(kwargs)[0]
54+
name = func.__name__
55+
56+
msg = (
57+
f"the '{param}' parameter is not "
58+
"supported in the pandas "
59+
fr"implementation of {name}\(\)"
60+
)
61+
with pytest.raises(ValueError, match=msg):
62+
func(ser, **kwargs)
63+
64+
65+
def test_validate_sum_initial():
66+
ser = Series([1, 2])
67+
msg = (
68+
r"the 'initial' parameter is not "
69+
r"supported in the pandas "
70+
r"implementation of sum\(\)"
71+
)
72+
with pytest.raises(ValueError, match=msg):
73+
np.sum(ser, initial=10)
74+
75+
76+
def test_validate_median_initial():
77+
ser = Series([1, 2])
78+
msg = (
79+
r"the 'overwrite_input' parameter is not "
80+
r"supported in the pandas "
81+
r"implementation of median\(\)"
82+
)
83+
with pytest.raises(ValueError, match=msg):
84+
# It seems like np.median doesn't dispatch, so we use the
85+
# method instead of the ufunc.
86+
ser.median(overwrite_input=True)
87+
88+
89+
def test_validate_stat_keepdims():
90+
ser = Series([1, 2])
91+
msg = (
92+
r"the 'keepdims' parameter is not "
93+
r"supported in the pandas "
94+
r"implementation of sum\(\)"
95+
)
96+
with pytest.raises(ValueError, match=msg):
97+
np.sum(ser, keepdims=True)

pandas/tests/test_lib.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._libs import lib, writers as libwriters
4+
from pandas._libs import Timestamp, lib, writers as libwriters
55

6-
import pandas as pd
76
from pandas import Index
87
import pandas._testing as tm
98

@@ -41,7 +40,7 @@ def test_fast_unique_multiple_list_gen_sort(self):
4140
tm.assert_numpy_array_equal(np.array(out), expected)
4241

4342
def test_fast_unique_multiple_unsortable_runtimewarning(self):
44-
arr = [np.array(["foo", pd.Timestamp("2000")])]
43+
arr = [np.array(["foo", Timestamp("2000")])]
4544
with tm.assert_produces_warning(RuntimeWarning):
4645
lib.fast_unique_multiple(arr, sort=None)
4746

pandas/tests/test_sorting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def verify_order(df):
298298
"outer": np.ones(len(out), dtype="bool"),
299299
}
300300

301-
for how in "left", "right", "outer", "inner":
301+
for how in ["left", "right", "outer", "inner"]:
302302
mask = jmask[how]
303303
frame = align(out[mask].copy())
304304
assert mask.all() ^ mask.any() or how == "outer"

0 commit comments

Comments
 (0)