@@ -636,6 +636,31 @@ def test_mangled(self):
636
636
)
637
637
tm .assert_frame_equal (result , expected )
638
638
639
+ def test_agg_multiple_columns (self ):
640
+ df = pd .DataFrame ({"A" : [0 , 0 , 1 , 1 ], "B" : [1 , 2 , 3 , 4 ], "C" : [3 , 4 , 5 , 6 ]})
641
+ result = df .groupby ("A" ).agg (
642
+ add = (["B" , "C" ], lambda x : x ["B" ].max () + x ["C" ].min ()),
643
+ minus = (["C" , "B" ], lambda x : x ["B" ].max () - x ["C" ].min ())
644
+ )
645
+ expected = pd .DataFrame (
646
+ {"add" : [5 , 9 ], "minus" : [- 1 , - 1 ]}, index = pd .Index ([0 , 1 ], name = "A" )
647
+ )
648
+ tm .assert_frame_equal (result , expected )
649
+
650
+ def test_agg_multi_missing_column_raises (self ):
651
+ df = pd .DataFrame ({"A" : [0 , 0 , 1 , 1 ], "B" : [1 , 2 , 3 , 4 ], "C" : [3 , 4 , 5 , 6 ]})
652
+ with pytest .raises (KeyError , match = "Column 'D' does not exist" ):
653
+ df .groupby ("A" ).agg (
654
+ minus = (["D" , "C" ], lambda x : x ["D" ].max () - x ["C" ].min ()),
655
+ )
656
+
657
+ def test_agg_multi_missing_key_raises (self ):
658
+ df = pd .DataFrame ({"A" : [0 , 0 , 1 , 1 ], "B" : [1 , 2 , 3 , 4 ], "C" : [3 , 4 , 5 , 6 ]})
659
+ with pytest .raises (KeyError , match = "D" ):
660
+ df .groupby ("A" ).agg (
661
+ minus = (["B" , "C" ], lambda x : x ["D" ].max () - x ["D" ].min ()),
662
+ )
663
+
639
664
640
665
@pytest .mark .parametrize (
641
666
"agg_col1, agg_col2, agg_col3, agg_result1, agg_result2, agg_result3" ,
0 commit comments