diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 18ad5d90b3f60..9fbcced75c327 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -866,3 +866,17 @@ 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) + + +@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(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)