Skip to content

Commit 3494078

Browse files
authored
TST: dropping of nuisance columns for groupby ops #38815 (#43674)
1 parent 6a1c6b4 commit 3494078

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

pandas/tests/groupby/test_groupby.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -853,11 +853,6 @@ def test_groupby_multi_corner(df):
853853

854854
def test_omit_nuisance(df):
855855
grouped = df.groupby("A")
856-
857-
result = grouped.mean()
858-
expected = df.loc[:, ["A", "C", "D"]].groupby("A").mean()
859-
tm.assert_frame_equal(result, expected)
860-
861856
agged = grouped.agg(np.mean)
862857
exp = grouped.mean()
863858
tm.assert_frame_equal(agged, exp)
@@ -876,14 +871,43 @@ def test_omit_nuisance(df):
876871
grouped.agg(lambda x: x.sum(0, numeric_only=False))
877872

878873

879-
def test_omit_nuisance_sem(df):
880-
# GH 38774 - sem should work with nuisance columns
874+
@pytest.mark.parametrize(
875+
"agg_function",
876+
["max", "min"],
877+
)
878+
def test_keep_nuisance_agg(df, agg_function):
879+
# GH 38815
880+
grouped = df.groupby("A")
881+
result = getattr(grouped, agg_function)()
882+
expected = result.copy()
883+
expected.loc["bar", "B"] = getattr(df.loc[df["A"] == "bar", "B"], agg_function)()
884+
expected.loc["foo", "B"] = getattr(df.loc[df["A"] == "foo", "B"], agg_function)()
885+
tm.assert_frame_equal(result, expected)
886+
887+
888+
@pytest.mark.parametrize(
889+
"agg_function",
890+
["sum", "mean", "prod", "std", "var", "median"],
891+
)
892+
def test_omit_nuisance_agg(df, agg_function):
893+
# GH 38774, GH 38815
881894
grouped = df.groupby("A")
882-
result = grouped.sem()
883-
expected = df.loc[:, ["A", "C", "D"]].groupby("A").sem()
895+
result = getattr(grouped, agg_function)()
896+
expected = getattr(df.loc[:, ["A", "C", "D"]].groupby("A"), agg_function)()
884897
tm.assert_frame_equal(result, expected)
885898

886899

900+
def test_omit_nuisance_warnings(df):
901+
# GH 38815
902+
with tm.assert_produces_warning(
903+
FutureWarning, filter_level="always", check_stacklevel=False
904+
):
905+
grouped = df.groupby("A")
906+
result = grouped.skew()
907+
expected = df.loc[:, ["A", "C", "D"]].groupby("A").skew()
908+
tm.assert_frame_equal(result, expected)
909+
910+
887911
def test_omit_nuisance_python_multiple(three_group):
888912
grouped = three_group.groupby(["A", "B"])
889913

0 commit comments

Comments
 (0)