Skip to content

CLN: More pytest idioms in pandas/tests/window #36657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions pandas/tests/window/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,36 @@ def halflife_with_times(request):
def dtypes(request):
"""Dtypes for window tests"""
return request.param


@pytest.fixture(
params=[
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[1, 0]),
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[1, 1]),
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=["C", "C"]),
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[1.0, 0]),
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[0.0, 1]),
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=["C", 1]),
DataFrame([[2.0, 4.0], [1.0, 2.0], [5.0, 2.0], [8.0, 1.0]], columns=[1, 0.0]),
DataFrame([[2, 4.0], [1, 2.0], [5, 2.0], [8, 1.0]], columns=[0, 1.0]),
DataFrame([[2, 4], [1, 2], [5, 2], [8, 1.0]], columns=[1.0, "X"]),
]
)
def pairwise_frames(request):
"""Pairwise frames test_pairwise"""
return request.param


@pytest.fixture
def pairwise_target_frame():
"""Pairwise target frame for test_pairwise"""
return DataFrame([[2, 4], [1, 2], [5, 2], [8, 1]], columns=[0, 1])


@pytest.fixture
def pairwise_other_frame():
"""Pairwise other frame for test_pairwise"""
return DataFrame(
[[None, 1, 1], [None, 1, 2], [None, 3, 2], [None, 8, 1]],
columns=["Y", "Z", "X"],
)
2 changes: 0 additions & 2 deletions pandas/tests/window/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ def foo(x, par):
result = df.rolling(1).apply(foo, args=args_kwargs[0], kwargs=args_kwargs[1])
tm.assert_frame_equal(result, expected)

result = df.rolling(1).apply(foo, args=(10,))

midx = MultiIndex.from_tuples([(1, 0), (1, 1)], names=["gr", None])
expected = Series([11.0, 12.0], index=midx, name="a")

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/window/test_base_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ def test_rolling_forward_window(constructor, func, np_func, expected, np_kwargs)
match = "Forward-looking windows can't have center=True"
with pytest.raises(ValueError, match=match):
rolling = constructor(values).rolling(window=indexer, center=True)
result = getattr(rolling, func)()
getattr(rolling, func)()

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

rolling = constructor(values).rolling(window=indexer, min_periods=2)
result = getattr(rolling, func)()
Expand Down
23 changes: 15 additions & 8 deletions pandas/tests/window/test_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ def test_constructor(which):
c(min_periods=1, center=True)
c(min_periods=1, center=False)


@pytest.mark.parametrize("w", [2.0, "foo", np.array([2])])
@pytest.mark.filterwarnings(
"ignore:The `center` argument on `expanding` will be removed in the future"
)
def test_constructor_invalid(which, w):
# not valid
for w in [2.0, "foo", np.array([2])]:
msg = "min_periods must be an integer"
with pytest.raises(ValueError, match=msg):
c(min_periods=w)

msg = "center must be a boolean"
with pytest.raises(ValueError, match=msg):
c(min_periods=1, center=w)

c = which.expanding
msg = "min_periods must be an integer"
with pytest.raises(ValueError, match=msg):
c(min_periods=w)

msg = "center must be a boolean"
with pytest.raises(ValueError, match=msg):
c(min_periods=1, center=w)


@pytest.mark.parametrize("method", ["std", "mean", "sum", "max", "min", "var"])
Expand Down
97 changes: 54 additions & 43 deletions pandas/tests/window/test_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


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

Expand Down Expand Up @@ -55,19 +55,25 @@ def test_getitem_multiple(self):
result = r.B.count()
tm.assert_series_equal(result, expected)

def test_rolling(self):
@pytest.mark.parametrize(
"f", ["sum", "mean", "min", "max", "count", "kurt", "skew"]
)
def test_rolling(self, f):
g = self.frame.groupby("A")
r = g.rolling(window=4)

for f in ["sum", "mean", "min", "max", "count", "kurt", "skew"]:
result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.rolling(4), f)())
tm.assert_frame_equal(result, expected)
result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.rolling(4), f)())
tm.assert_frame_equal(result, expected)

for f in ["std", "var"]:
result = getattr(r, f)(ddof=1)
expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1))
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("f", ["std", "var"])
def test_rolling_ddof(self, f):
g = self.frame.groupby("A")
r = g.rolling(window=4)

result = getattr(r, f)(ddof=1)
expected = g.apply(lambda x: getattr(x.rolling(4), f)(ddof=1))
tm.assert_frame_equal(result, expected)

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

def test_rolling_corr_cov(self):
@pytest.mark.parametrize("f", ["corr", "cov"])
def test_rolling_corr_cov(self, f):
g = self.frame.groupby("A")
r = g.rolling(window=4)

for f in ["corr", "cov"]:
result = getattr(r, f)(self.frame)
result = getattr(r, f)(self.frame)

def func(x):
return getattr(x.rolling(4), f)(self.frame)
def func(x):
return getattr(x.rolling(4), f)(self.frame)

expected = g.apply(func)
tm.assert_frame_equal(result, expected)
expected = g.apply(func)
tm.assert_frame_equal(result, expected)

result = getattr(r.B, f)(pairwise=True)
result = getattr(r.B, f)(pairwise=True)

def func(x):
return getattr(x.B.rolling(4), f)(pairwise=True)
def func(x):
return getattr(x.B.rolling(4), f)(pairwise=True)

expected = g.apply(func)
tm.assert_series_equal(result, expected)
expected = g.apply(func)
tm.assert_series_equal(result, expected)

def test_rolling_apply(self, raw):
g = self.frame.groupby("A")
Expand Down Expand Up @@ -134,20 +140,25 @@ def test_rolling_apply_mutability(self):
result = g.rolling(window=2).sum()
tm.assert_frame_equal(result, expected)

def test_expanding(self):
@pytest.mark.parametrize(
"f", ["sum", "mean", "min", "max", "count", "kurt", "skew"]
)
def test_expanding(self, f):
g = self.frame.groupby("A")
r = g.expanding()

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

result = getattr(r, f)()
expected = g.apply(lambda x: getattr(x.expanding(), f)())
tm.assert_frame_equal(result, expected)
@pytest.mark.parametrize("f", ["std", "var"])
def test_expanding_ddof(self, f):
g = self.frame.groupby("A")
r = g.expanding()

for f in ["std", "var"]:
result = getattr(r, f)(ddof=0)
expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0))
tm.assert_frame_equal(result, expected)
result = getattr(r, f)(ddof=0)
expected = g.apply(lambda x: getattr(x.expanding(), f)(ddof=0))
tm.assert_frame_equal(result, expected)

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

def test_expanding_corr_cov(self):
@pytest.mark.parametrize("f", ["corr", "cov"])
def test_expanding_corr_cov(self, f):
g = self.frame.groupby("A")
r = g.expanding()

for f in ["corr", "cov"]:
result = getattr(r, f)(self.frame)
result = getattr(r, f)(self.frame)

def func(x):
return getattr(x.expanding(), f)(self.frame)
def func(x):
return getattr(x.expanding(), f)(self.frame)

expected = g.apply(func)
tm.assert_frame_equal(result, expected)
expected = g.apply(func)
tm.assert_frame_equal(result, expected)

result = getattr(r.B, f)(pairwise=True)
result = getattr(r.B, f)(pairwise=True)

def func(x):
return getattr(x.B.expanding(), f)(pairwise=True)
def func(x):
return getattr(x.B.expanding(), f)(pairwise=True)

expected = g.apply(func)
tm.assert_series_equal(result, expected)
expected = g.apply(func)
tm.assert_series_equal(result, expected)

def test_expanding_apply(self, raw):
g = self.frame.groupby("A")
Expand Down
Loading