Skip to content

Commit 0d9e53d

Browse files
gfyoungproost
authored andcommitted
TST: Test named aggregations with functions (pandas-dev#29262)
1 parent db989d4 commit 0d9e53d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/tests/groupby/aggregate/test_aggregate.py

+27
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,33 @@ def test_mangled(self):
495495
tm.assert_frame_equal(result, expected)
496496

497497

498+
def myfunc(s):
499+
return np.percentile(s, q=0.90)
500+
501+
502+
@pytest.mark.parametrize("func", [lambda s: np.percentile(s, q=0.90), myfunc])
503+
def test_lambda_named_agg(func):
504+
# see gh-28467
505+
animals = DataFrame(
506+
{
507+
"kind": ["cat", "dog", "cat", "dog"],
508+
"height": [9.1, 6.0, 9.5, 34.0],
509+
"weight": [7.9, 7.5, 9.9, 198.0],
510+
}
511+
)
512+
513+
result = animals.groupby("kind").agg(
514+
mean_height=("height", "mean"), perc90=("height", func)
515+
)
516+
expected = DataFrame(
517+
[[9.3, 9.1036], [20.0, 6.252]],
518+
columns=["mean_height", "perc90"],
519+
index=Index(["cat", "dog"], name="kind"),
520+
)
521+
522+
tm.assert_frame_equal(result, expected)
523+
524+
498525
class TestLambdaMangling:
499526
def test_maybe_mangle_lambdas_passthrough(self):
500527
assert _maybe_mangle_lambdas("mean") == "mean"

0 commit comments

Comments
 (0)