Skip to content

TST: dropping of nuisance columns for groupby ops #38815 #43674

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 7 commits into from
Oct 5, 2021
42 changes: 33 additions & 9 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,11 +853,6 @@ def test_groupby_multi_corner(df):

def test_omit_nuisance(df):
grouped = df.groupby("A")

result = grouped.mean()
expected = df.loc[:, ["A", "C", "D"]].groupby("A").mean()
tm.assert_frame_equal(result, expected)

agged = grouped.agg(np.mean)
exp = grouped.mean()
tm.assert_frame_equal(agged, exp)
Expand All @@ -876,14 +871,43 @@ def test_omit_nuisance(df):
grouped.agg(lambda x: x.sum(0, numeric_only=False))


def test_omit_nuisance_sem(df):
# GH 38774 - sem should work with nuisance columns
@pytest.mark.parametrize(
"agg_function",
["max", "min"],
)
def test_keep_nuisance_agg(df, agg_function):
# GH 38815
grouped = df.groupby("A")
result = getattr(grouped, agg_function)()
expected = result.copy()
expected.loc["bar", "B"] = getattr(df.loc[df["A"] == "bar", "B"], agg_function)()
expected.loc["foo", "B"] = getattr(df.loc[df["A"] == "foo", "B"], agg_function)()
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"agg_function",
["sum", "mean", "prod", "std", "var", "median"],
)
def test_omit_nuisance_agg(df, agg_function):
# GH 38774, GH 38815
grouped = df.groupby("A")
result = grouped.sem()
expected = df.loc[:, ["A", "C", "D"]].groupby("A").sem()
result = getattr(grouped, agg_function)()
expected = getattr(df.loc[:, ["A", "C", "D"]].groupby("A"), agg_function)()
tm.assert_frame_equal(result, expected)


def test_omit_nuisance_warnings(df):
# GH 38815
with tm.assert_produces_warning(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this warning? why are you adding extra options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the warning that you wanted to be asserted. I took away the parameterization now

https://github.com/pandas-dev/pandas/pull/43674/checks?check_run_id=3771738681
need sto assert this warning happens (this is pretty new btw)

FutureWarning, filter_level="always", check_stacklevel=False
):
grouped = df.groupby("A")
result = grouped.skew()
expected = df.loc[:, ["A", "C", "D"]].groupby("A").skew()
tm.assert_frame_equal(result, expected)


def test_omit_nuisance_python_multiple(three_group):
grouped = three_group.groupby(["A", "B"])

Expand Down