diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 3de6af8eef694..83b096cfc2d05 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -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) @@ -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( + 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"])