@@ -863,11 +863,6 @@ def test_groupby_multi_corner(df):
863
863
864
864
def test_omit_nuisance (df ):
865
865
grouped = df .groupby ("A" )
866
-
867
- result = grouped .mean ()
868
- expected = df .loc [:, ["A" , "C" , "D" ]].groupby ("A" ).mean ()
869
- tm .assert_frame_equal (result , expected )
870
-
871
866
agged = grouped .agg (np .mean )
872
867
exp = grouped .mean ()
873
868
tm .assert_frame_equal (agged , exp )
@@ -886,14 +881,43 @@ def test_omit_nuisance(df):
886
881
grouped .agg (lambda x : x .sum (0 , numeric_only = False ))
887
882
888
883
889
- def test_omit_nuisance_sem (df ):
890
- # GH 38774 - sem should work with nuisance columns
884
+ @pytest .mark .parametrize (
885
+ "agg_function" ,
886
+ ["max" , "min" ],
887
+ )
888
+ def test_keep_nuisance_agg (df , agg_function ):
889
+ # GH 38815
890
+ grouped = df .groupby ("A" )
891
+ result = getattr (grouped , agg_function )()
892
+ expected = result .copy ()
893
+ expected .loc ["bar" , "B" ] = getattr (df .loc [df ["A" ] == "bar" , "B" ], agg_function )()
894
+ expected .loc ["foo" , "B" ] = getattr (df .loc [df ["A" ] == "foo" , "B" ], agg_function )()
895
+ tm .assert_frame_equal (result , expected )
896
+
897
+
898
+ @pytest .mark .parametrize (
899
+ "agg_function" ,
900
+ ["sum" , "mean" , "prod" , "std" , "var" , "median" ],
901
+ )
902
+ def test_omit_nuisance_agg (df , agg_function ):
903
+ # GH 38774, GH 38815
891
904
grouped = df .groupby ("A" )
892
- result = grouped . sem ()
893
- expected = df .loc [:, ["A" , "C" , "D" ]].groupby ("A" ). sem ()
905
+ result = getattr ( grouped , agg_function ) ()
906
+ expected = getattr ( df .loc [:, ["A" , "C" , "D" ]].groupby ("A" ), agg_function ) ()
894
907
tm .assert_frame_equal (result , expected )
895
908
896
909
910
+ def test_omit_nuisance_warnings (df ):
911
+ # GH 38815
912
+ with tm .assert_produces_warning (
913
+ FutureWarning , filter_level = "always" , check_stacklevel = False
914
+ ):
915
+ grouped = df .groupby ("A" )
916
+ result = grouped .skew ()
917
+ expected = df .loc [:, ["A" , "C" , "D" ]].groupby ("A" ).skew ()
918
+ tm .assert_frame_equal (result , expected )
919
+
920
+
897
921
def test_omit_nuisance_python_multiple (three_group ):
898
922
grouped = three_group .groupby (["A" , "B" ])
899
923
0 commit comments