From f0a449e72535172b958a2099b00fab70f041c6df Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 29 Oct 2019 04:46:15 +0000 Subject: [PATCH 1/2] TST: Test named aggregations with functions Closes https://github.com/pandas-dev/pandas/issues/28467 --- .../tests/groupby/aggregate/test_aggregate.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index aa80c461a00e7..f9a4acb12499f 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -494,6 +494,52 @@ def test_mangled(self): ) tm.assert_frame_equal(result, expected) + def test_lambda_named_agg(self): + # see gh-28467 + animals = DataFrame( + { + "kind": ["cat", "dog", "cat", "dog"], + "height": [9.1, 6.0, 9.5, 34.0], + "weight": [7.9, 7.5, 9.9, 198.0], + } + ) + + result = animals.groupby("kind").agg( + mean_height=("height", "mean"), + perc90=("height", lambda s: np.percentile(s, q=0.90)), + ) + expected = DataFrame( + [[9.3, 9.1036], [20.0, 6.252]], + columns=["mean_height", "perc90"], + index=Index(["cat", "dog"], name="kind"), + ) + + tm.assert_frame_equal(result, expected) + + def test_func_named_agg(self): + # see gh-28467 + def myfunc(s): + return np.percentile(s, q=0.90) + + animals = DataFrame( + { + "kind": ["cat", "dog", "cat", "dog"], + "height": [9.1, 6.0, 9.5, 34.0], + "weight": [7.9, 7.5, 9.9, 198.0], + } + ) + + result = animals.groupby("kind").agg( + mean_height=("height", "mean"), perc90=("height", myfunc) + ) + expected = DataFrame( + [[9.3, 9.1036], [20.0, 6.252]], + columns=["mean_height", "perc90"], + index=Index(["cat", "dog"], name="kind"), + ) + + tm.assert_frame_equal(result, expected) + class TestLambdaMangling: def test_maybe_mangle_lambdas_passthrough(self): From f11fcb16f04a9e3fc0f2423f6c527e200e50bccf Mon Sep 17 00:00:00 2001 From: gfyoung Date: Tue, 29 Oct 2019 06:55:11 +0000 Subject: [PATCH 2/2] MAINT: Use pytest idiom for added tests --- .../tests/groupby/aggregate/test_aggregate.py | 61 +++++++------------ 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index f9a4acb12499f..b56da5fba6f80 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -494,51 +494,32 @@ def test_mangled(self): ) tm.assert_frame_equal(result, expected) - def test_lambda_named_agg(self): - # see gh-28467 - animals = DataFrame( - { - "kind": ["cat", "dog", "cat", "dog"], - "height": [9.1, 6.0, 9.5, 34.0], - "weight": [7.9, 7.5, 9.9, 198.0], - } - ) - result = animals.groupby("kind").agg( - mean_height=("height", "mean"), - perc90=("height", lambda s: np.percentile(s, q=0.90)), - ) - expected = DataFrame( - [[9.3, 9.1036], [20.0, 6.252]], - columns=["mean_height", "perc90"], - index=Index(["cat", "dog"], name="kind"), - ) - - tm.assert_frame_equal(result, expected) +def myfunc(s): + return np.percentile(s, q=0.90) - def test_func_named_agg(self): - # see gh-28467 - def myfunc(s): - return np.percentile(s, q=0.90) - animals = DataFrame( - { - "kind": ["cat", "dog", "cat", "dog"], - "height": [9.1, 6.0, 9.5, 34.0], - "weight": [7.9, 7.5, 9.9, 198.0], - } - ) +@pytest.mark.parametrize("func", [lambda s: np.percentile(s, q=0.90), myfunc]) +def test_lambda_named_agg(func): + # see gh-28467 + animals = DataFrame( + { + "kind": ["cat", "dog", "cat", "dog"], + "height": [9.1, 6.0, 9.5, 34.0], + "weight": [7.9, 7.5, 9.9, 198.0], + } + ) - result = animals.groupby("kind").agg( - mean_height=("height", "mean"), perc90=("height", myfunc) - ) - expected = DataFrame( - [[9.3, 9.1036], [20.0, 6.252]], - columns=["mean_height", "perc90"], - index=Index(["cat", "dog"], name="kind"), - ) + result = animals.groupby("kind").agg( + mean_height=("height", "mean"), perc90=("height", func) + ) + expected = DataFrame( + [[9.3, 9.1036], [20.0, 6.252]], + columns=["mean_height", "perc90"], + index=Index(["cat", "dog"], name="kind"), + ) - tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) class TestLambdaMangling: