forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reductions.py
224 lines (171 loc) · 6.4 KB
/
test_reductions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import numpy as np
import pytest
import pandas as pd
from pandas import Series
import pandas._testing as tm
@pytest.mark.parametrize("operation, expected", [("min", "a"), ("max", "b")])
def test_reductions_series_strings(operation, expected):
# GH#31746
ser = Series(["a", "b"], dtype="string")
res_operation_serie = getattr(ser, operation)()
assert res_operation_serie == expected
@pytest.mark.parametrize("as_period", [True, False])
def test_mode_extension_dtype(as_period):
# GH#41927 preserve dt64tz dtype
ser = Series([pd.Timestamp(1979, 4, n) for n in range(1, 5)])
if as_period:
ser = ser.dt.to_period("D")
else:
ser = ser.dt.tz_localize("US/Central")
res = ser.mode()
assert res.dtype == ser.dtype
tm.assert_series_equal(res, ser)
def test_mode_nullable_dtype(any_numeric_ea_dtype):
# GH#55340
ser = Series([1, 3, 2, pd.NA, 3, 2, pd.NA], dtype=any_numeric_ea_dtype)
result = ser.mode(dropna=False)
expected = Series([2, 3, pd.NA], dtype=any_numeric_ea_dtype)
tm.assert_series_equal(result, expected)
result = ser.mode(dropna=True)
expected = Series([2, 3], dtype=any_numeric_ea_dtype)
tm.assert_series_equal(result, expected)
ser[-1] = pd.NA
result = ser.mode(dropna=True)
expected = Series([2, 3], dtype=any_numeric_ea_dtype)
tm.assert_series_equal(result, expected)
result = ser.mode(dropna=False)
expected = Series([pd.NA], dtype=any_numeric_ea_dtype)
tm.assert_series_equal(result, expected)
def test_mode_infer_string():
# GH#56183
pytest.importorskip("pyarrow")
ser = Series(["a", "b"], dtype=object)
with pd.option_context("future.infer_string", True):
result = ser.mode()
expected = Series(["a", "b"], dtype=object)
tm.assert_series_equal(result, expected)
def test_reductions_td64_with_nat():
# GH#8617
ser = Series([0, pd.NaT], dtype="m8[ns]")
exp = ser[0]
assert ser.median() == exp
assert ser.min() == exp
assert ser.max() == exp
def test_td64_sum_empty(skipna):
# GH#37151
ser = Series([], dtype="timedelta64[ns]")
result = ser.sum(skipna=skipna)
assert isinstance(result, pd.Timedelta)
assert result == pd.Timedelta(0)
def test_td64_summation_overflow():
# GH#9442
ser = Series(pd.date_range("20130101", periods=100000, freq="h"))
ser[0] += pd.Timedelta("1s 1ms")
# mean
result = (ser - ser.min()).mean()
expected = pd.Timedelta((pd.TimedeltaIndex(ser - ser.min()).asi8 / len(ser)).sum())
# the computation is converted to float so
# might be some loss of precision
assert np.allclose(result._value / 1000, expected._value / 1000)
# sum
msg = "overflow in timedelta operation"
with pytest.raises(ValueError, match=msg):
(ser - ser.min()).sum()
s1 = ser[0:10000]
with pytest.raises(ValueError, match=msg):
(s1 - s1.min()).sum()
s2 = ser[0:1000]
(s2 - s2.min()).sum()
def test_prod_numpy16_bug():
ser = Series([1.0, 1.0, 1.0], index=range(3))
result = ser.prod()
assert not isinstance(result, Series)
@pytest.mark.parametrize("nan_val", [np.nan, pd.NA])
def test_object_sum_allna(nan_val):
# GH#60229
ser = Series([nan_val] * 5, dtype=object)
result = ser.sum(axis=0, skipna=True)
expected = np.nan
tm.assert_equal(result, expected)
result = ser.sum(axis=0, skipna=False)
expected = nan_val
tm.assert_equal(result, expected)
@pytest.mark.parametrize("func", [np.any, np.all])
@pytest.mark.parametrize("kwargs", [{"keepdims": True}, {"out": object()}])
def test_validate_any_all_out_keepdims_raises(kwargs, func):
ser = Series([1, 2])
param = next(iter(kwargs))
name = func.__name__
msg = (
f"the '{param}' parameter is not "
"supported in the pandas "
rf"implementation of {name}\(\)"
)
with pytest.raises(ValueError, match=msg):
func(ser, **kwargs)
def test_validate_sum_initial():
ser = Series([1, 2])
msg = (
r"the 'initial' parameter is not "
r"supported in the pandas "
r"implementation of sum\(\)"
)
with pytest.raises(ValueError, match=msg):
np.sum(ser, initial=10)
def test_validate_median_initial():
ser = Series([1, 2])
msg = (
r"the 'overwrite_input' parameter is not "
r"supported in the pandas "
r"implementation of median\(\)"
)
with pytest.raises(ValueError, match=msg):
# It seems like np.median doesn't dispatch, so we use the
# method instead of the ufunc.
ser.median(overwrite_input=True)
def test_validate_stat_keepdims():
ser = Series([1, 2])
msg = (
r"the 'keepdims' parameter is not "
r"supported in the pandas "
r"implementation of sum\(\)"
)
with pytest.raises(ValueError, match=msg):
np.sum(ser, keepdims=True)
def test_mean_with_convertible_string_raises():
# GH#44008
ser = Series(["1", "2"])
assert ser.sum() == "12"
msg = "Could not convert string '12' to numeric|does not support|Cannot perform"
with pytest.raises(TypeError, match=msg):
ser.mean()
df = ser.to_frame()
msg = r"Could not convert \['12'\] to numeric|does not support|Cannot perform"
with pytest.raises(TypeError, match=msg):
df.mean()
def test_mean_dont_convert_j_to_complex():
# GH#36703
df = pd.DataFrame([{"db": "J", "numeric": 123}])
msg = r"Could not convert \['J'\] to numeric|does not support|Cannot perform"
with pytest.raises(TypeError, match=msg):
df.mean()
with pytest.raises(TypeError, match=msg):
df.agg("mean")
msg = "Could not convert string 'J' to numeric|does not support|Cannot perform"
with pytest.raises(TypeError, match=msg):
df["db"].mean()
msg = "Could not convert string 'J' to numeric|ufunc 'divide'|Cannot perform"
with pytest.raises(TypeError, match=msg):
np.mean(df["db"].astype("string").array)
def test_median_with_convertible_string_raises():
# GH#34671 this _could_ return a string "2", but definitely not float 2.0
msg = r"Cannot convert \['1' '2' '3'\] to numeric|does not support|Cannot perform"
ser = Series(["1", "2", "3"])
with pytest.raises(TypeError, match=msg):
ser.median()
msg = (
r"Cannot convert \[\['1' '2' '3'\]\] to numeric|does not support|Cannot perform"
)
df = ser.to_frame()
with pytest.raises(TypeError, match=msg):
df.median()