@@ -494,6 +494,52 @@ def test_mangled(self):
494
494
)
495
495
tm .assert_frame_equal (result , expected )
496
496
497
+ def test_lambda_named_agg (self ):
498
+ # see gh-28467
499
+ animals = DataFrame (
500
+ {
501
+ "kind" : ["cat" , "dog" , "cat" , "dog" ],
502
+ "height" : [9.1 , 6.0 , 9.5 , 34.0 ],
503
+ "weight" : [7.9 , 7.5 , 9.9 , 198.0 ],
504
+ }
505
+ )
506
+
507
+ result = animals .groupby ("kind" ).agg (
508
+ mean_height = ("height" , "mean" ),
509
+ perc90 = ("height" , lambda s : np .percentile (s , q = 0.90 )),
510
+ )
511
+ expected = DataFrame (
512
+ [[9.3 , 9.1036 ], [20.0 , 6.252 ]],
513
+ columns = ["mean_height" , "perc90" ],
514
+ index = Index (["cat" , "dog" ], name = "kind" ),
515
+ )
516
+
517
+ tm .assert_frame_equal (result , expected )
518
+
519
+ def test_func_named_agg (self ):
520
+ # see gh-28467
521
+ def myfunc (s ):
522
+ return np .percentile (s , q = 0.90 )
523
+
524
+ animals = DataFrame (
525
+ {
526
+ "kind" : ["cat" , "dog" , "cat" , "dog" ],
527
+ "height" : [9.1 , 6.0 , 9.5 , 34.0 ],
528
+ "weight" : [7.9 , 7.5 , 9.9 , 198.0 ],
529
+ }
530
+ )
531
+
532
+ result = animals .groupby ("kind" ).agg (
533
+ mean_height = ("height" , "mean" ), perc90 = ("height" , myfunc )
534
+ )
535
+ expected = DataFrame (
536
+ [[9.3 , 9.1036 ], [20.0 , 6.252 ]],
537
+ columns = ["mean_height" , "perc90" ],
538
+ index = Index (["cat" , "dog" ], name = "kind" ),
539
+ )
540
+
541
+ tm .assert_frame_equal (result , expected )
542
+
497
543
498
544
class TestLambdaMangling :
499
545
def test_maybe_mangle_lambdas_passthrough (self ):
0 commit comments