From c0d89590aeb7a9ec6b884bc0504301cf638b984a Mon Sep 17 00:00:00 2001 From: Bruno Costa Date: Tue, 7 Dec 2021 17:09:03 -0300 Subject: [PATCH 1/2] Test for Group By - Apply Key Error --- pandas/tests/groupby/test_apply.py | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index e95ab700e12d3..ffe5ecc847b38 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -1177,3 +1177,34 @@ def test_apply_empty_string_nan_coerce_bug(): index=MultiIndex.from_tuples([(1, ""), (2, "")], names=["a", "b"]), ) tm.assert_frame_equal(result, expected) + + +@pytest.mark.parametrize("is_float", [True, False]) +def test_apply_index_key_error_bug(is_float): + # GH 44310 + if is_float: + result = DataFrame( + { + "a": ["aa", "a2", "a3"], + "b": [1, 2, 3], + }, + index=Index([1.0, 2.0, 3.0]), + ) + else: + result = DataFrame( + { + "a": ["aa", "a2", "a3"], + "b": [1, 2, 3], + }, + index=Index([1, 2, 3]), + ) + expected = DataFrame( + { + "b_mean": [2.0, 3.0, 1.0], + }, + index=Index(["a2", "a3", "aa"], name="a"), + ) + result = result.groupby("a").apply( + lambda df: Series([df["b"].mean()], index=["b_mean"]) + ) + tm.assert_frame_equal(result, expected) From c2fdb5b76ac42b654b0b68ce010a096b1dfcc569 Mon Sep 17 00:00:00 2001 From: ThunderSly Date: Fri, 10 Dec 2021 15:40:17 -0300 Subject: [PATCH 2/2] Paramaters change --- pandas/tests/groupby/test_apply.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index ffe5ecc847b38..e81a902b84780 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -1179,25 +1179,16 @@ def test_apply_empty_string_nan_coerce_bug(): tm.assert_frame_equal(result, expected) -@pytest.mark.parametrize("is_float", [True, False]) -def test_apply_index_key_error_bug(is_float): +@pytest.mark.parametrize("index_values", [[1, 2, 3], [1.0, 2.0, 3.0]]) +def test_apply_index_key_error_bug(index_values): # GH 44310 - if is_float: - result = DataFrame( - { - "a": ["aa", "a2", "a3"], - "b": [1, 2, 3], - }, - index=Index([1.0, 2.0, 3.0]), - ) - else: - result = DataFrame( - { - "a": ["aa", "a2", "a3"], - "b": [1, 2, 3], - }, - index=Index([1, 2, 3]), - ) + result = DataFrame( + { + "a": ["aa", "a2", "a3"], + "b": [1, 2, 3], + }, + index=Index(index_values), + ) expected = DataFrame( { "b_mean": [2.0, 3.0, 1.0],