Skip to content

Commit 7896056

Browse files
committed
TST: Test named aggregations with functions
Closes #28467
1 parent 8562dcc commit 7896056

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

pandas/tests/groupby/aggregate/test_aggregate.py

+39
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,45 @@ def test_mangled(self):
494494
)
495495
tm.assert_frame_equal(result, expected)
496496

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

498537
class TestLambdaMangling:
499538
def test_maybe_mangle_lambdas_passthrough(self):

0 commit comments

Comments
 (0)