Skip to content

Commit 9666fff

Browse files
mroeschkeMatt Roeschke
authored and
Kevin D Smith
committed
CLN: More pytest idioms in pandas/tests/window (pandas-dev#36657)
* Clean test_timeseries_window * CLN: More pytest idioms to pandas/tests/window Co-authored-by: Matt Roeschke <[email protected]>
1 parent 5ddc625 commit 9666fff

9 files changed

+295
-224
lines changed

pandas/tests/window/conftest.py

+33
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,36 @@ def halflife_with_times(request):
344344
def dtypes(request):
345345
"""Dtypes for window tests"""
346346
return request.param
347+
348+
349+
@pytest.fixture(
350+
params=[
351+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[1, 0]),
352+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[1, 1]),
353+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=["C", "C"]),
354+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[1.0, 0]),
355+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[0.0, 1]),
356+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=["C", 1]),
357+
DataFrame([[2.0, 4.0], [1.0, 2.0], [5.0, 2.0], [8.0, 1.0]], columns=[1, 0.0]),
358+
DataFrame([[2, 4.0], [1, 2.0], [5, 2.0], [8, 1.0]], columns=[0, 1.0]),
359+
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1.0]], columns=[1.0, "X"]),
360+
]
361+
)
362+
def pairwise_frames(request):
363+
"""Pairwise frames test_pairwise"""
364+
return request.param
365+
366+
367+
@pytest.fixture
368+
def pairwise_target_frame():
369+
"""Pairwise target frame for test_pairwise"""
370+
return DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[0, 1])
371+
372+
373+
@pytest.fixture
374+
def pairwise_other_frame():
375+
"""Pairwise other frame for test_pairwise"""
376+
return DataFrame(
377+
[[None, 1, 1], [None, 1, 2], [None, 3, 2], [None, 8, 1]],
378+
columns=["Y", "Z", "X"],
379+
)

pandas/tests/window/test_apply.py

-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ def foo(x, par):
155155
result = df.rolling(1).apply(foo, args=args_kwargs[0], kwargs=args_kwargs[1])
156156
tm.assert_frame_equal(result, expected)
157157

158-
result = df.rolling(1).apply(foo, args=(10,))
159-
160158
midx = MultiIndex.from_tuples([(1, 0), (1, 1)], names=["gr", None])
161159
expected = Series([11.0, 12.0], index=midx, name="a")
162160

pandas/tests/window/test_base_indexer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ def test_rolling_forward_window(constructor, func, np_func, expected, np_kwargs)
148148
match = "Forward-looking windows can't have center=True"
149149
with pytest.raises(ValueError, match=match):
150150
rolling = constructor(values).rolling(window=indexer, center=True)
151-
result = getattr(rolling, func)()
151+
getattr(rolling, func)()
152152

153153
match = "Forward-looking windows don't support setting the closed argument"
154154
with pytest.raises(ValueError, match=match):
155155
rolling = constructor(values).rolling(window=indexer, closed="right")
156-
result = getattr(rolling, func)()
156+
getattr(rolling, func)()
157157

158158
rolling = constructor(values).rolling(window=indexer, min_periods=2)
159159
result = getattr(rolling, func)()

pandas/tests/window/test_expanding.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,22 @@ def test_constructor(which):
2929
c(min_periods=1, center=True)
3030
c(min_periods=1, center=False)
3131

32+
33+
@pytest.mark.parametrize("w", [2.0, "foo", np.array([2])])
34+
@pytest.mark.filterwarnings(
35+
"ignore:The `center` argument on `expanding` will be removed in the future"
36+
)
37+
def test_constructor_invalid(which, w):
3238
# not valid
33-
for w in [2.0, "foo", np.array([2])]:
34-
msg = "min_periods must be an integer"
35-
with pytest.raises(ValueError, match=msg):
36-
c(min_periods=w)
37-
38-
msg = "center must be a boolean"
39-
with pytest.raises(ValueError, match=msg):
40-
c(min_periods=1, center=w)
39+
40+
c = which.expanding
41+
msg = "min_periods must be an integer"
42+
with pytest.raises(ValueError, match=msg):
43+
c(min_periods=w)
44+
45+
msg = "center must be a boolean"
46+
with pytest.raises(ValueError, match=msg):
47+
c(min_periods=1, center=w)
4148

4249

4350
@pytest.mark.parametrize("method", ["std", "mean", "sum", "max", "min", "var"])

pandas/tests/window/test_grouper.py

+54-43
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class TestGrouperGrouping:
11-
def setup_method(self, method):
11+
def setup_method(self):
1212
self.series = Series(np.arange(10))
1313
self.frame = DataFrame({"A": [1] * 20 + [2] * 12 + [3] * 8, "B": np.arange(40)})
1414

@@ -55,19 +55,25 @@ def test_getitem_multiple(self):
5555
result = r.B.count()
5656
tm.assert_series_equal(result, expected)
5757

58-
def test_rolling(self):
58+
@pytest.mark.parametrize(
59+
"f", ["sum", "mean", "min", "max", "count", "kurt", "skew"]
60+
)
61+
def test_rolling(self, f):
5962
g = self.frame.groupby("A")
6063
r = g.rolling(window=4)
6164

62-
for f in ["sum", "mean", "min", "max", "count", "kurt", "skew"]:
63-
result = getattr(r, f)()
64-
expected = g.apply(lambda x: getattr(x.rolling(4), f)())
65-
tm.assert_frame_equal(result, expected)
65+
result = getattr(r, f)()
66+
expected = g.apply(lambda x: getattr(x.rolling(4), f)())
67+
tm.assert_frame_equal(result, expected)
6668

67-
for f in ["std", "var"]:
68-
result = getattr(r, f)(ddof=1)
69-
expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1))
70-
tm.assert_frame_equal(result, expected)
69+
@pytest.mark.parametrize("f", ["std", "var"])
70+
def test_rolling_ddof(self, f):
71+
g = self.frame.groupby("A")
72+
r = g.rolling(window=4)
73+
74+
result = getattr(r, f)(ddof=1)
75+
expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1))
76+
tm.assert_frame_equal(result, expected)
7177

7278
@pytest.mark.parametrize(
7379
"interpolation", ["linear", "lower", "higher", "midpoint", "nearest"]
@@ -81,26 +87,26 @@ def test_rolling_quantile(self, interpolation):
8187
)
8288
tm.assert_frame_equal(result, expected)
8389

84-
def test_rolling_corr_cov(self):
90+
@pytest.mark.parametrize("f", ["corr", "cov"])
91+
def test_rolling_corr_cov(self, f):
8592
g = self.frame.groupby("A")
8693
r = g.rolling(window=4)
8794

88-
for f in ["corr", "cov"]:
89-
result = getattr(r, f)(self.frame)
95+
result = getattr(r, f)(self.frame)
9096

91-
def func(x):
92-
return getattr(x.rolling(4), f)(self.frame)
97+
def func(x):
98+
return getattr(x.rolling(4), f)(self.frame)
9399

94-
expected = g.apply(func)
95-
tm.assert_frame_equal(result, expected)
100+
expected = g.apply(func)
101+
tm.assert_frame_equal(result, expected)
96102

97-
result = getattr(r.B, f)(pairwise=True)
103+
result = getattr(r.B, f)(pairwise=True)
98104

99-
def func(x):
100-
return getattr(x.B.rolling(4), f)(pairwise=True)
105+
def func(x):
106+
return getattr(x.B.rolling(4), f)(pairwise=True)
101107

102-
expected = g.apply(func)
103-
tm.assert_series_equal(result, expected)
108+
expected = g.apply(func)
109+
tm.assert_series_equal(result, expected)
104110

105111
def test_rolling_apply(self, raw):
106112
g = self.frame.groupby("A")
@@ -134,20 +140,25 @@ def test_rolling_apply_mutability(self):
134140
result = g.rolling(window=2).sum()
135141
tm.assert_frame_equal(result, expected)
136142

137-
def test_expanding(self):
143+
@pytest.mark.parametrize(
144+
"f", ["sum", "mean", "min", "max", "count", "kurt", "skew"]
145+
)
146+
def test_expanding(self, f):
138147
g = self.frame.groupby("A")
139148
r = g.expanding()
140149

141-
for f in ["sum", "mean", "min", "max", "count", "kurt", "skew"]:
150+
result = getattr(r, f)()
151+
expected = g.apply(lambda x: getattr(x.expanding(), f)())
152+
tm.assert_frame_equal(result, expected)
142153

143-
result = getattr(r, f)()
144-
expected = g.apply(lambda x: getattr(x.expanding(), f)())
145-
tm.assert_frame_equal(result, expected)
154+
@pytest.mark.parametrize("f", ["std", "var"])
155+
def test_expanding_ddof(self, f):
156+
g = self.frame.groupby("A")
157+
r = g.expanding()
146158

147-
for f in ["std", "var"]:
148-
result = getattr(r, f)(ddof=0)
149-
expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0))
150-
tm.assert_frame_equal(result, expected)
159+
result = getattr(r, f)(ddof=0)
160+
expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0))
161+
tm.assert_frame_equal(result, expected)
151162

152163
@pytest.mark.parametrize(
153164
"interpolation", ["linear", "lower", "higher", "midpoint", "nearest"]
@@ -161,26 +172,26 @@ def test_expanding_quantile(self, interpolation):
161172
)
162173
tm.assert_frame_equal(result, expected)
163174

164-
def test_expanding_corr_cov(self):
175+
@pytest.mark.parametrize("f", ["corr", "cov"])
176+
def test_expanding_corr_cov(self, f):
165177
g = self.frame.groupby("A")
166178
r = g.expanding()
167179

168-
for f in ["corr", "cov"]:
169-
result = getattr(r, f)(self.frame)
180+
result = getattr(r, f)(self.frame)
170181

171-
def func(x):
172-
return getattr(x.expanding(), f)(self.frame)
182+
def func(x):
183+
return getattr(x.expanding(), f)(self.frame)
173184

174-
expected = g.apply(func)
175-
tm.assert_frame_equal(result, expected)
185+
expected = g.apply(func)
186+
tm.assert_frame_equal(result, expected)
176187

177-
result = getattr(r.B, f)(pairwise=True)
188+
result = getattr(r.B, f)(pairwise=True)
178189

179-
def func(x):
180-
return getattr(x.B.expanding(), f)(pairwise=True)
190+
def func(x):
191+
return getattr(x.B.expanding(), f)(pairwise=True)
181192

182-
expected = g.apply(func)
183-
tm.assert_series_equal(result, expected)
193+
expected = g.apply(func)
194+
tm.assert_series_equal(result, expected)
184195

185196
def test_expanding_apply(self, raw):
186197
g = self.frame.groupby("A")

0 commit comments

Comments
 (0)