Skip to content

Commit be70726

Browse files
committed
TST: Test named aggregations with functions
Closes pandas-dev#28467
1 parent 8562dcc commit be70726

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,43 @@ def test_mangled(self):
494494
)
495495
tm.assert_frame_equal(result, expected)
496496

497+
def test_func_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([[9.3, 9.1036], [20.0, 6.252]],
509+
columns=["mean_height", "perc90"],
510+
index=Index(["cat", "dog"], name="kind"))
511+
512+
tm.assert_frame_equal(result, expected)
513+
514+
def test_lambda_named_agg2(self):
515+
# see gh-28467
516+
def myfunc(s):
517+
return np.percentile(s, q=0.90)
518+
519+
animals = DataFrame({
520+
"kind": ["cat", "dog", "cat", "dog"],
521+
"height": [9.1, 6.0, 9.5, 34.0],
522+
"weight": [7.9, 7.5, 9.9, 198.0]
523+
})
524+
525+
result = animals.groupby("kind").agg(
526+
mean_height=("height", "mean"),
527+
perc90=("height", myfunc))
528+
expected = DataFrame([[9.3, 9.1036], [20.0, 6.252]],
529+
columns=["mean_height", "perc90"],
530+
index=Index(["cat", "dog"], name="kind"))
531+
532+
tm.assert_frame_equal(result, expected)
533+
497534

498535
class TestLambdaMangling:
499536
def test_maybe_mangle_lambdas_passthrough(self):

0 commit comments

Comments
 (0)