Skip to content

Commit 49f4a94

Browse files
authored
TST: Add regression test for groupby().apply() external mutation (#63126)
1 parent 4d83483 commit 49f4a94

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

pandas/tests/groupby/test_apply.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,3 +1516,28 @@ def f(x):
15161516
).set_index(["cat1", "cat2"])["rank"]
15171517
result = df.groupby("cat1").apply(f)
15181518
tm.assert_series_equal(result, expected)
1519+
1520+
1521+
def test_groupby_apply_store_copy():
1522+
# GH40673
1523+
rng = np.random.default_rng(seed=42)
1524+
1525+
df = DataFrame(
1526+
{
1527+
"A": rng.normal(10, 12, size=(4,)),
1528+
"B": [1, 2, 1, 2],
1529+
}
1530+
)
1531+
1532+
store = {}
1533+
1534+
def addstore(x):
1535+
store[len(store)] = x.copy()
1536+
1537+
df.groupby("B").apply(addstore)
1538+
1539+
expected_out_0 = df.iloc[[0, 2], [0]]
1540+
expected_out_1 = df.iloc[[1, 3], [0]]
1541+
1542+
tm.assert_frame_equal(store[0], expected_out_0)
1543+
tm.assert_frame_equal(store[1], expected_out_1)

0 commit comments

Comments
 (0)