@@ -853,11 +853,6 @@ def test_groupby_multi_corner(df):
853
853
854
854
def test_omit_nuisance (df ):
855
855
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
-
861
856
agged = grouped .agg (np .mean )
862
857
exp = grouped .mean ()
863
858
tm .assert_frame_equal (agged , exp )
@@ -876,14 +871,43 @@ def test_omit_nuisance(df):
876
871
grouped .agg (lambda x : x .sum (0 , numeric_only = False ))
877
872
878
873
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
881
894
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 ) ()
884
897
tm .assert_frame_equal (result , expected )
885
898
886
899
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
+
887
911
def test_omit_nuisance_python_multiple (three_group ):
888
912
grouped = three_group .groupby (["A" , "B" ])
889
913
0 commit comments