-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_reduce.py
178 lines (136 loc) · 5.64 KB
/
test_reduce.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
import numpy as np
import pandas as pd
import pytest
from _pytest.fixtures import SubRequest
from rle_array import RLEDtype
pytestmark = pytest.mark.filterwarnings("ignore:performance")
@pytest.fixture(params=["single", "multi", "empty", "sparse"])
def data_orig(request: SubRequest) -> pd.Series:
if request.param == "single":
return pd.Series([1], dtype=int)
elif request.param == "multi":
return pd.Series([1, 1, 2, 3, 1, 1], dtype=int)
elif request.param == "empty":
return pd.Series([], dtype=int)
elif request.param == "sparse":
return pd.Series([1, 1, np.nan, np.nan, 1, 1], dtype=float)
else:
raise ValueError(f"Unknown data type: {request.param}")
@pytest.fixture(params=["single", "multi", "empty"])
def data_orig_bool(request: SubRequest) -> pd.Series:
if request.param == "single":
return pd.Series([False], dtype=bool)
elif request.param == "multi":
return pd.Series([False, False, True, False], dtype=bool)
elif request.param == "empty":
return pd.Series([], dtype=bool)
else:
raise ValueError(f"Unknown data type: {request.param}")
@pytest.fixture
def data_rle(data_orig: pd.Series) -> pd.Series:
return data_orig.astype(RLEDtype(data_orig.dtype))
@pytest.fixture
def data_rle_bool(data_orig_bool: pd.Series) -> pd.Series:
return data_orig_bool.astype(RLEDtype(data_orig_bool.dtype))
@pytest.fixture(params=[True, False])
def skipna(request: SubRequest) -> bool:
b = request.param
assert isinstance(b, bool)
return b
@pytest.fixture(
params=["min", "max", "mean", "median", "prod", "skew", "std", "sum", "var", "kurt"]
)
def name(request: SubRequest) -> str:
n = request.param
assert isinstance(n, str)
return n
@pytest.fixture(params=["any", "all"])
def name_bool(request: SubRequest) -> str:
n = request.param
assert isinstance(n, str)
return n
@pytest.fixture(params=["max", "mean", "median", "min", "prod", "std", "sum", "var"])
def numpy_op(request: SubRequest) -> str:
n = request.param
assert isinstance(n, str)
return n
@pytest.fixture(params=["all", "any"])
def numpy_op_bool(request: SubRequest) -> str:
op = request.param
assert isinstance(op, str)
return op
@pytest.fixture(params=["mean", "std", "var"])
def numpy_op_with_dtype(request: SubRequest) -> str:
op = request.param
assert isinstance(op, str)
return op
def test_reduce(
data_orig: pd.Series, data_rle: pd.Series, skipna: bool, name: str
) -> None:
f_orig = getattr(data_orig, name)
f_rle = getattr(data_rle, name)
result_orig = f_orig(skipna=skipna)
result_rle = f_rle(skipna=skipna)
assert (
(np.isnan(result_orig) & np.isnan(result_rle)) | (result_orig == result_rle)
).all()
# don't check type here since pandas does some magic casting from numpy to python
def test_reduce_bool(
data_orig_bool: pd.Series, data_rle_bool: pd.Series, name_bool: str
) -> None:
f_orig = getattr(data_orig_bool, name_bool)
f_rle = getattr(data_rle_bool, name_bool)
result_orig = f_orig()
result_rle = f_rle()
assert (result_orig == result_rle).all()
# don't check type here since pandas does some magic casting from numpy to python
def test_array_numpy_bool_axis_notimplemented(
data_rle_bool: pd.Series, numpy_op_bool: str
) -> None:
f = getattr(data_rle_bool.array, numpy_op_bool)
with pytest.raises(NotImplementedError, match="Only axis=0 is supported."):
f(axis=2)
def test_array_numpy_bool_out_notimplemented(
data_rle_bool: pd.Series, numpy_op_bool: str
) -> None:
f = getattr(data_rle_bool.array, numpy_op_bool)
out = data_rle_bool.array.copy()
with pytest.raises(NotImplementedError, match="out parameter is not supported."):
f(out=out)
def test_array_reduction_not_implemented(data_rle: pd.Series) -> None:
with pytest.raises(NotImplementedError, match="reduction foo is not implemented."):
data_rle.array._reduce(name="foo")
def test_array_numpy_bool(
data_orig_bool: pd.Series, data_rle_bool: pd.Series, numpy_op_bool: str
) -> None:
f = getattr(np, numpy_op_bool)
result_orig = f(data_rle_bool.array)
result_rle = f(data_rle_bool.array)
assert result_orig == result_rle
assert type(result_orig) == type(result_rle)
def test_array_numpy(data_orig: pd.Series, data_rle: pd.Series, numpy_op: str) -> None:
f = getattr(np, numpy_op)
result_orig = f(data_orig.array)
result_rle = f(data_rle.array)
assert (pd.isna(result_orig) and pd.isna(result_rle)) or (result_orig == result_rle)
if len(data_orig) > 0:
assert type(result_orig) == type(result_rle)
else:
# pandas might use pd.NA, while we still use float, see https://github.com/pandas-dev/pandas/issues/35475
if isinstance(result_orig, type(pd.NA)):
assert type(result_rle) == float
else:
assert type(result_orig) == type(result_rle)
def test_array_numpy_axis_notimplemented(data_rle: pd.Series, numpy_op: str) -> None:
f = getattr(data_rle.array, numpy_op)
with pytest.raises(NotImplementedError, match="Only axis=0 is supported."):
f(axis=2)
def test_array_numpy_out_notimplemented(data_rle: pd.Series, numpy_op: str) -> None:
f = getattr(data_rle.array, numpy_op)
out = data_rle.array.copy()
with pytest.raises(NotImplementedError, match="out parameter is not supported."):
f(out=out)
def test_array_numpy_dtype(data_rle: pd.Series, numpy_op_with_dtype: str) -> None:
f = getattr(np, numpy_op_with_dtype)
with pytest.raises(NotImplementedError, match="dtype parameter is not supported."):
f(data_rle.array, dtype=np.float16)