Skip to content

Commit 69da020

Browse files
committed
Add new testcase for assignment fails with enable Copy-on-Write
1 parent 6921ebb commit 69da020

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pandas/tests/indexing/test_iloc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
date_range,
2323
interval_range,
2424
isna,
25+
option_context,
2526
to_datetime,
2627
)
2728
import pandas._testing as tm
@@ -1448,3 +1449,26 @@ def test_iloc_nullable_int64_size_1_nan(self):
14481449
result = DataFrame({"a": ["test"], "b": [np.nan]})
14491450
with pytest.raises(TypeError, match="Invalid value"):
14501451
result.loc[:, "b"] = result.loc[:, "b"].astype("Int64")
1452+
1453+
def test_iloc_setitem_list_with_cow(self):
1454+
# GH#60309
1455+
with option_context("mode.copy_on_write", True):
1456+
dftest = DataFrame(
1457+
{"A": [1, 4, 1, 5], "B": [2, 5, 2, 6], "C": [3, 6, 1, 7]}
1458+
)
1459+
df = dftest[["B", "C"]]
1460+
1461+
# Perform the iloc operation
1462+
df.iloc[[1, 3], :] = [[2, 2], [2, 2]]
1463+
1464+
# Check that original DataFrame is unchanged
1465+
expected_orig = DataFrame(
1466+
{"A": [1, 4, 1, 5], "B": [2, 5, 2, 6], "C": [3, 6, 1, 7]}
1467+
)
1468+
tm.assert_frame_equal(dftest, expected_orig)
1469+
1470+
# Check that view is modified correctly
1471+
expected_view = DataFrame(
1472+
{"B": [2, 2, 2, 2], "C": [3, 2, 1, 2]}, index=df.index
1473+
)
1474+
tm.assert_frame_equal(df, expected_view)

0 commit comments

Comments
 (0)