From 5a381c35503a6a5a60daf5ffde50dbac3334d6c6 Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 28 Mar 2020 13:43:47 +0100 Subject: [PATCH 1/2] TEST: Add test for #22541 to ensure that error does not occur again --- pandas/tests/groupby/test_apply.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 18ad5d90b3f60..e2585593bb4d2 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -866,3 +866,16 @@ def fct(group): [[1.0, 2.0], [3.0], [np.nan]], index=pd.Index(["a", "b", "none"], name="A") ) tm.assert_series_equal(result, expected) + + +def test_apply_function_index_return(): + # GH: 22541 + df = pd.DataFrame([1, 2, 2, 2, 1, 2, 3, 1, 3, 1], columns=["id"]) + result = df.groupby("id").apply(lambda gr: gr.index) + result_2 = df.groupby("id").apply(lambda gr: gr.index + 1 - 1) + expected = pd.Series( + [pd.Index([0, 4, 7, 9]), pd.Index([1, 2, 3, 5]), pd.Index([6, 8])], + index=pd.Index([1, 2, 3], name="id"), + ) + tm.assert_series_equal(result, expected) + tm.assert_series_equal(result, result_2) From 0b7caf79775be7aa7da5f912f8013e1be1f130d6 Mon Sep 17 00:00:00 2001 From: phofl Date: Sun, 29 Mar 2020 20:07:50 +0200 Subject: [PATCH 2/2] BUG: Parametrize unittest --- pandas/tests/groupby/test_apply.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index e2585593bb4d2..9fbcced75c327 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -868,14 +868,15 @@ def fct(group): tm.assert_series_equal(result, expected) -def test_apply_function_index_return(): +@pytest.mark.parametrize( + "function", [lambda gr: gr.index, lambda gr: gr.index + 1 - 1], +) +def test_apply_function_index_return(function): # GH: 22541 df = pd.DataFrame([1, 2, 2, 2, 1, 2, 3, 1, 3, 1], columns=["id"]) - result = df.groupby("id").apply(lambda gr: gr.index) - result_2 = df.groupby("id").apply(lambda gr: gr.index + 1 - 1) + result = df.groupby("id").apply(function) expected = pd.Series( [pd.Index([0, 4, 7, 9]), pd.Index([1, 2, 3, 5]), pd.Index([6, 8])], index=pd.Index([1, 2, 3], name="id"), ) tm.assert_series_equal(result, expected) - tm.assert_series_equal(result, result_2)