|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +from pandas.errors import SettingWithCopyWarning |
6 | 7 | import pandas.util._test_decorators as td
|
7 | 8 |
|
8 | 9 | from pandas.core.dtypes.base import _registry as ea_registry
|
@@ -1400,6 +1401,94 @@ def test_frame_setitem_empty_dataframe(self):
|
1400 | 1401 | )
|
1401 | 1402 | tm.assert_frame_equal(df, expected)
|
1402 | 1403 |
|
| 1404 | + def test_iloc_setitem_view_2dblock(self, using_copy_on_write, warn_copy_on_write): |
| 1405 | + # https://github.com/pandas-dev/pandas/issues/60309 |
| 1406 | + df_parent = DataFrame( |
| 1407 | + { |
| 1408 | + "A": [1, 4, 1, 5], |
| 1409 | + "B": [2, 5, 2, 6], |
| 1410 | + "C": [3, 6, 1, 7], |
| 1411 | + "D": [8, 9, 10, 11], |
| 1412 | + } |
| 1413 | + ) |
| 1414 | + df_orig = df_parent.copy() |
| 1415 | + df = df_parent[["B", "C"]] |
| 1416 | + |
| 1417 | + # Perform the iloc operation |
| 1418 | + if using_copy_on_write: |
| 1419 | + df.iloc[[1, 3], :] = [[2, 2], [2, 2]] |
| 1420 | + |
| 1421 | + # Check that original DataFrame is unchanged |
| 1422 | + tm.assert_frame_equal(df_parent, df_orig) |
| 1423 | + elif warn_copy_on_write: |
| 1424 | + # TODO(COW): should this warn? |
| 1425 | + # with tm.assert_cow_warning(warn_copy_on_write): |
| 1426 | + df.iloc[[1, 3], :] = [[2, 2], [2, 2]] |
| 1427 | + else: |
| 1428 | + with pd.option_context("chained_assignment", "warn"): |
| 1429 | + with tm.assert_produces_warning(SettingWithCopyWarning): |
| 1430 | + df.iloc[[1, 3], :] = [[2, 2], [2, 2]] |
| 1431 | + |
| 1432 | + # Check that df is modified correctly |
| 1433 | + expected = DataFrame({"B": [2, 2, 2, 2], "C": [3, 2, 1, 2]}, index=df.index) |
| 1434 | + tm.assert_frame_equal(df, expected) |
| 1435 | + |
| 1436 | + # with setting to subset of columns |
| 1437 | + df = df_parent[["B", "C", "D"]] |
| 1438 | + if using_copy_on_write or warn_copy_on_write: |
| 1439 | + df.iloc[[1, 3], 0:3:2] = [[2, 2], [2, 2]] |
| 1440 | + tm.assert_frame_equal(df_parent, df_orig) |
| 1441 | + else: |
| 1442 | + with pd.option_context("chained_assignment", "warn"): |
| 1443 | + with tm.assert_produces_warning(SettingWithCopyWarning): |
| 1444 | + df.iloc[[1, 3], 0:3:2] = [[2, 2], [2, 2]] |
| 1445 | + |
| 1446 | + expected = DataFrame( |
| 1447 | + {"B": [2, 2, 2, 2], "C": [3, 6, 1, 7], "D": [8, 2, 10, 2]}, index=df.index |
| 1448 | + ) |
| 1449 | + tm.assert_frame_equal(df, expected) |
| 1450 | + |
| 1451 | + @pytest.mark.parametrize( |
| 1452 | + "indexer, value", |
| 1453 | + [ |
| 1454 | + (([0, 2], slice(None)), [[2, 2, 2, 2], [2, 2, 2, 2]]), |
| 1455 | + ((slice(None), slice(None)), 2), |
| 1456 | + ((0, [1, 3]), [2, 2]), |
| 1457 | + (([0], 1), [2]), |
| 1458 | + (([0], np.int64(1)), [2]), |
| 1459 | + ((slice(None), np.int64(1)), [2, 2, 2]), |
| 1460 | + ((slice(None, 2), np.int64(1)), [2, 2]), |
| 1461 | + ( |
| 1462 | + (np.array([False, True, False]), np.array([False, True, False, True])), |
| 1463 | + [2, 2], |
| 1464 | + ), |
| 1465 | + ], |
| 1466 | + ) |
| 1467 | + def test_setitem_2dblock_with_ref( |
| 1468 | + self, indexer, value, using_copy_on_write, warn_copy_on_write |
| 1469 | + ): |
| 1470 | + # https://github.com/pandas-dev/pandas/issues/60309 |
| 1471 | + arr = np.arange(12).reshape(3, 4) |
| 1472 | + |
| 1473 | + df_parent = DataFrame(arr.copy(), columns=list("ABCD")) |
| 1474 | + # the test is specifically for the case where the df is backed by a single |
| 1475 | + # block (taking the non-split path) |
| 1476 | + assert df_parent._mgr.is_single_block |
| 1477 | + df_orig = df_parent.copy() |
| 1478 | + df = df_parent[:] |
| 1479 | + |
| 1480 | + with tm.assert_cow_warning(warn_copy_on_write): |
| 1481 | + df.iloc[indexer] = value |
| 1482 | + |
| 1483 | + # Check that original DataFrame is unchanged |
| 1484 | + if using_copy_on_write: |
| 1485 | + tm.assert_frame_equal(df_parent, df_orig) |
| 1486 | + |
| 1487 | + # Check that df is modified correctly |
| 1488 | + arr[indexer] = value |
| 1489 | + expected = DataFrame(arr, columns=list("ABCD")) |
| 1490 | + tm.assert_frame_equal(df, expected) |
| 1491 | + |
1403 | 1492 |
|
1404 | 1493 | def test_full_setter_loc_incompatible_dtype():
|
1405 | 1494 | # https://github.com/pandas-dev/pandas/issues/55791
|
|
0 commit comments