Skip to content

Commit 31ea45f

Browse files
authored
BUG: Add test to ensure, that bug will not occur again. #33058 (#33072)
1 parent f334fcc commit 31ea45f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pandas/tests/groupby/test_apply.py

+21
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,24 @@ def test_apply_function_index_return(function):
880880
index=pd.Index([1, 2, 3], name="id"),
881881
)
882882
tm.assert_series_equal(result, expected)
883+
884+
885+
def test_apply_function_with_indexing():
886+
# GH: 33058
887+
df = pd.DataFrame(
888+
{"col1": ["A", "A", "A", "B", "B", "B"], "col2": [1, 2, 3, 4, 5, 6]}
889+
)
890+
891+
def fn(x):
892+
x.col2[x.index[-1]] = 0
893+
return x.col2
894+
895+
result = df.groupby(["col1"], as_index=False).apply(fn)
896+
expected = pd.Series(
897+
[1, 2, 0, 4, 5, 0],
898+
index=pd.MultiIndex.from_tuples(
899+
[(0, 0), (0, 1), (0, 2), (1, 3), (1, 4), (1, 5)]
900+
),
901+
name="col2",
902+
)
903+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)