Skip to content

Commit 9a03f9c

Browse files
authored
CLN: tests/window/moments (#44961)
* CLN: tests/window/moments * Tighten xfails * Change to module scope * Change other to module scope * Remove scope
1 parent 091c9a0 commit 9a03f9c

File tree

4 files changed

+367
-355
lines changed

4 files changed

+367
-355
lines changed

pandas/tests/window/moments/conftest.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,50 @@
1010
)
1111

1212

13-
# create the data only once as we are not setting it
14-
def _create_consistency_data():
15-
def create_series():
16-
return [
17-
Series(dtype=np.float64, name="a"),
18-
Series([np.nan] * 5),
19-
Series([1.0] * 5),
20-
Series(range(5, 0, -1)),
21-
Series(range(5)),
22-
Series([np.nan, 1.0, np.nan, 1.0, 1.0]),
23-
Series([np.nan, 1.0, np.nan, 2.0, 3.0]),
24-
Series([np.nan, 1.0, np.nan, 3.0, 2.0]),
25-
]
26-
27-
def create_dataframes():
28-
return [
29-
DataFrame(columns=["a", "a"]),
30-
DataFrame(np.arange(15).reshape((5, 3)), columns=["a", "a", 99]),
31-
] + [DataFrame(s) for s in create_series()]
32-
33-
def is_constant(x):
34-
values = x.values.ravel("K")
35-
return len(set(values[notna(values)])) == 1
36-
37-
def no_nans(x):
38-
return x.notna().all().all()
39-
13+
def create_series():
4014
return [
41-
(x, is_constant(x), no_nans(x))
42-
for x in itertools.chain(create_dataframes(), create_dataframes())
15+
Series(dtype=np.float64, name="a"),
16+
Series([np.nan] * 5),
17+
Series([1.0] * 5),
18+
Series(range(5, 0, -1)),
19+
Series(range(5)),
20+
Series([np.nan, 1.0, np.nan, 1.0, 1.0]),
21+
Series([np.nan, 1.0, np.nan, 2.0, 3.0]),
22+
Series([np.nan, 1.0, np.nan, 3.0, 2.0]),
4323
]
4424

4525

46-
@pytest.fixture(params=_create_consistency_data())
47-
def consistency_data(request):
26+
def create_dataframes():
27+
return [
28+
DataFrame(columns=["a", "a"]),
29+
DataFrame(np.arange(15).reshape((5, 3)), columns=["a", "a", 99]),
30+
] + [DataFrame(s) for s in create_series()]
31+
32+
33+
def is_constant(x):
34+
values = x.values.ravel("K")
35+
return len(set(values[notna(values)])) == 1
36+
37+
38+
@pytest.fixture(
39+
params=(
40+
obj
41+
for obj in itertools.chain(create_series(), create_dataframes())
42+
if is_constant(obj)
43+
),
44+
scope="module",
45+
)
46+
def consistent_data(request):
47+
return request.param
48+
49+
50+
@pytest.fixture(params=create_series())
51+
def series_data(request):
52+
return request.param
53+
54+
55+
@pytest.fixture(params=itertools.chain(create_series(), create_dataframes()))
56+
def all_data(request):
4857
"""
4958
Test:
5059
- Empty Series / DataFrame

pandas/tests/window/moments/test_moments_consistency_ewm.py

Lines changed: 100 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create_mock_weights(obj, com, adjust, ignore_na):
3030

3131

3232
def create_mock_series_weights(s, com, adjust, ignore_na):
33-
w = Series(np.nan, index=s.index)
33+
w = Series(np.nan, index=s.index, name=s.name)
3434
alpha = 1.0 / (1.0 + com)
3535
if adjust:
3636
count = 0
@@ -58,63 +58,66 @@ def create_mock_series_weights(s, com, adjust, ignore_na):
5858
return w
5959

6060

61-
def test_ewm_consistency_mean(consistency_data, adjust, ignore_na, min_periods):
62-
x, is_constant, no_nans = consistency_data
61+
def test_ewm_consistency_mean(all_data, adjust, ignore_na, min_periods):
6362
com = 3.0
6463

65-
result = x.ewm(
64+
result = all_data.ewm(
6665
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
6766
).mean()
68-
weights = create_mock_weights(x, com=com, adjust=adjust, ignore_na=ignore_na)
67+
weights = create_mock_weights(all_data, com=com, adjust=adjust, ignore_na=ignore_na)
6968
expected = (
70-
x.multiply(weights).cumsum().divide(weights.cumsum()).fillna(method="ffill")
69+
all_data.multiply(weights)
70+
.cumsum()
71+
.divide(weights.cumsum())
72+
.fillna(method="ffill")
7173
)
7274
expected[
73-
x.expanding().count() < (max(min_periods, 1) if min_periods else 1)
75+
all_data.expanding().count() < (max(min_periods, 1) if min_periods else 1)
7476
] = np.nan
7577
tm.assert_equal(result, expected.astype("float64"))
7678

7779

78-
def test_ewm_consistency_consistent(consistency_data, adjust, ignore_na, min_periods):
79-
x, is_constant, no_nans = consistency_data
80+
def test_ewm_consistency_consistent(consistent_data, adjust, ignore_na, min_periods):
8081
com = 3.0
8182

82-
if is_constant:
83-
count_x = x.expanding().count()
84-
mean_x = x.ewm(
85-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
86-
).mean()
87-
# check that correlation of a series with itself is either 1 or NaN
88-
corr_x_x = x.ewm(
89-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
90-
).corr(x)
91-
exp = x.max() if isinstance(x, Series) else x.max().max()
83+
count_x = consistent_data.expanding().count()
84+
mean_x = consistent_data.ewm(
85+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
86+
).mean()
87+
# check that correlation of a series with itself is either 1 or NaN
88+
corr_x_x = consistent_data.ewm(
89+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
90+
).corr(consistent_data)
91+
exp = (
92+
consistent_data.max()
93+
if isinstance(consistent_data, Series)
94+
else consistent_data.max().max()
95+
)
9296

93-
# check mean of constant series
94-
expected = x * np.nan
95-
expected[count_x >= max(min_periods, 1)] = exp
96-
tm.assert_equal(mean_x, expected)
97+
# check mean of constant series
98+
expected = consistent_data * np.nan
99+
expected[count_x >= max(min_periods, 1)] = exp
100+
tm.assert_equal(mean_x, expected)
97101

98-
# check correlation of constant series with itself is NaN
99-
expected[:] = np.nan
100-
tm.assert_equal(corr_x_x, expected)
102+
# check correlation of constant series with itself is NaN
103+
expected[:] = np.nan
104+
tm.assert_equal(corr_x_x, expected)
101105

102106

103107
def test_ewm_consistency_var_debiasing_factors(
104-
consistency_data, adjust, ignore_na, min_periods
108+
all_data, adjust, ignore_na, min_periods
105109
):
106-
x, is_constant, no_nans = consistency_data
107110
com = 3.0
108111

109112
# check variance debiasing factors
110-
var_unbiased_x = x.ewm(
113+
var_unbiased_x = all_data.ewm(
111114
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
112115
).var(bias=False)
113-
var_biased_x = x.ewm(
116+
var_biased_x = all_data.ewm(
114117
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
115118
).var(bias=True)
116119

117-
weights = create_mock_weights(x, com=com, adjust=adjust, ignore_na=ignore_na)
120+
weights = create_mock_weights(all_data, com=com, adjust=adjust, ignore_na=ignore_na)
118121
cum_sum = weights.cumsum().fillna(method="ffill")
119122
cum_sum_sq = (weights * weights).cumsum().fillna(method="ffill")
120123
numerator = cum_sum * cum_sum
@@ -126,24 +129,21 @@ def test_ewm_consistency_var_debiasing_factors(
126129

127130

128131
@pytest.mark.parametrize("bias", [True, False])
129-
def test_moments_consistency_var(
130-
consistency_data, adjust, ignore_na, min_periods, bias
131-
):
132-
x, is_constant, no_nans = consistency_data
132+
def test_moments_consistency_var(all_data, adjust, ignore_na, min_periods, bias):
133133
com = 3.0
134134

135-
mean_x = x.ewm(
135+
mean_x = all_data.ewm(
136136
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
137137
).mean()
138-
var_x = x.ewm(
138+
var_x = all_data.ewm(
139139
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
140140
).var(bias=bias)
141141
assert not (var_x < 0).any().any()
142142

143143
if bias:
144144
# check that biased var(x) == mean(x^2) - mean(x)^2
145145
mean_x2 = (
146-
(x * x)
146+
(all_data * all_data)
147147
.ewm(com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na)
148148
.mean()
149149
)
@@ -152,45 +152,42 @@ def test_moments_consistency_var(
152152

153153
@pytest.mark.parametrize("bias", [True, False])
154154
def test_moments_consistency_var_constant(
155-
consistency_data, adjust, ignore_na, min_periods, bias
155+
consistent_data, adjust, ignore_na, min_periods, bias
156156
):
157-
x, is_constant, no_nans = consistency_data
158157
com = 3.0
159-
if is_constant:
160-
count_x = x.expanding(min_periods=min_periods).count()
161-
var_x = x.ewm(
162-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
163-
).var(bias=bias)
158+
count_x = consistent_data.expanding(min_periods=min_periods).count()
159+
var_x = consistent_data.ewm(
160+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
161+
).var(bias=bias)
164162

165-
# check that variance of constant series is identically 0
166-
assert not (var_x > 0).any().any()
167-
expected = x * np.nan
168-
expected[count_x >= max(min_periods, 1)] = 0.0
169-
if not bias:
170-
expected[count_x < 2] = np.nan
171-
tm.assert_equal(var_x, expected)
163+
# check that variance of constant series is identically 0
164+
assert not (var_x > 0).any().any()
165+
expected = consistent_data * np.nan
166+
expected[count_x >= max(min_periods, 1)] = 0.0
167+
if not bias:
168+
expected[count_x < 2] = np.nan
169+
tm.assert_equal(var_x, expected)
172170

173171

174172
@pytest.mark.parametrize("bias", [True, False])
175-
def test_ewm_consistency_std(consistency_data, adjust, ignore_na, min_periods, bias):
176-
x, is_constant, no_nans = consistency_data
173+
def test_ewm_consistency_std(all_data, adjust, ignore_na, min_periods, bias):
177174
com = 3.0
178-
var_x = x.ewm(
175+
var_x = all_data.ewm(
179176
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
180177
).var(bias=bias)
181178
assert not (var_x < 0).any().any()
182179

183-
std_x = x.ewm(
180+
std_x = all_data.ewm(
184181
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
185182
).std(bias=bias)
186183
assert not (std_x < 0).any().any()
187184

188185
# check that var(x) == std(x)^2
189186
tm.assert_equal(var_x, std_x * std_x)
190187

191-
cov_x_x = x.ewm(
188+
cov_x_x = all_data.ewm(
192189
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
193-
).cov(x, bias=bias)
190+
).cov(all_data, bias=bias)
194191
assert not (cov_x_x < 0).any().any()
195192

196193
# check that var(x) == cov(x, x)
@@ -199,57 +196,53 @@ def test_ewm_consistency_std(consistency_data, adjust, ignore_na, min_periods, b
199196

200197
@pytest.mark.parametrize("bias", [True, False])
201198
def test_ewm_consistency_series_cov_corr(
202-
consistency_data, adjust, ignore_na, min_periods, bias
199+
series_data, adjust, ignore_na, min_periods, bias
203200
):
204-
x, is_constant, no_nans = consistency_data
205201
com = 3.0
206202

207-
if isinstance(x, Series):
208-
var_x_plus_y = (
209-
(x + x)
210-
.ewm(com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na)
211-
.var(bias=bias)
212-
)
213-
var_x = x.ewm(
214-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
215-
).var(bias=bias)
216-
var_y = x.ewm(
217-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
218-
).var(bias=bias)
219-
cov_x_y = x.ewm(
220-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
221-
).cov(x, bias=bias)
222-
# check that cov(x, y) == (var(x+y) - var(x) -
223-
# var(y)) / 2
224-
tm.assert_equal(cov_x_y, 0.5 * (var_x_plus_y - var_x - var_y))
225-
226-
# check that corr(x, y) == cov(x, y) / (std(x) *
227-
# std(y))
228-
corr_x_y = x.ewm(
229-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
230-
).corr(x, bias=bias)
231-
std_x = x.ewm(
203+
var_x_plus_y = (
204+
(series_data + series_data)
205+
.ewm(com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na)
206+
.var(bias=bias)
207+
)
208+
var_x = series_data.ewm(
209+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
210+
).var(bias=bias)
211+
var_y = series_data.ewm(
212+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
213+
).var(bias=bias)
214+
cov_x_y = series_data.ewm(
215+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
216+
).cov(series_data, bias=bias)
217+
# check that cov(x, y) == (var(x+y) - var(x) -
218+
# var(y)) / 2
219+
tm.assert_equal(cov_x_y, 0.5 * (var_x_plus_y - var_x - var_y))
220+
221+
# check that corr(x, y) == cov(x, y) / (std(x) *
222+
# std(y))
223+
corr_x_y = series_data.ewm(
224+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
225+
).corr(series_data, bias=bias)
226+
std_x = series_data.ewm(
227+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
228+
).std(bias=bias)
229+
std_y = series_data.ewm(
230+
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
231+
).std(bias=bias)
232+
tm.assert_equal(corr_x_y, cov_x_y / (std_x * std_y))
233+
234+
if bias:
235+
# check that biased cov(x, y) == mean(x*y) -
236+
# mean(x)*mean(y)
237+
mean_x = series_data.ewm(
232238
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
233-
).std(bias=bias)
234-
std_y = x.ewm(
239+
).mean()
240+
mean_y = series_data.ewm(
235241
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
236-
).std(bias=bias)
237-
tm.assert_equal(corr_x_y, cov_x_y / (std_x * std_y))
238-
239-
if bias:
240-
# check that biased cov(x, y) == mean(x*y) -
241-
# mean(x)*mean(y)
242-
mean_x = x.ewm(
243-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
244-
).mean()
245-
mean_y = x.ewm(
246-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
247-
).mean()
248-
mean_x_times_y = (
249-
(x * x)
250-
.ewm(
251-
com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na
252-
)
253-
.mean()
254-
)
255-
tm.assert_equal(cov_x_y, mean_x_times_y - (mean_x * mean_y))
242+
).mean()
243+
mean_x_times_y = (
244+
(series_data * series_data)
245+
.ewm(com=com, min_periods=min_periods, adjust=adjust, ignore_na=ignore_na)
246+
.mean()
247+
)
248+
tm.assert_equal(cov_x_y, mean_x_times_y - (mean_x * mean_y))

0 commit comments

Comments
 (0)