|
22 | 22 | date_range, |
23 | 23 | interval_range, |
24 | 24 | isna, |
| 25 | + option_context, |
25 | 26 | to_datetime, |
26 | 27 | ) |
27 | 28 | import pandas._testing as tm |
@@ -1448,3 +1449,26 @@ def test_iloc_nullable_int64_size_1_nan(self): |
1448 | 1449 | result = DataFrame({"a": ["test"], "b": [np.nan]}) |
1449 | 1450 | with pytest.raises(TypeError, match="Invalid value"): |
1450 | 1451 | 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