Skip to content

Commit aae1b6f

Browse files
update tests for non-CoW and warn mode
1 parent 55a9698 commit aae1b6f

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

pandas/tests/frame/indexing/test_indexing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ def test_set_2d_casting_date_to_int(self, col, indexer):
15001500
)
15011501
tm.assert_frame_equal(df, expected)
15021502

1503+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
15031504
@pytest.mark.parametrize("has_ref", [True, False])
15041505
@pytest.mark.parametrize("col", [{}, {"name": "a"}])
15051506
def test_loc_setitem_reordering_with_all_true_indexer(self, col, has_ref):

pandas/tests/frame/indexing/test_setitem.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.errors import SettingWithCopyWarning
67
import pandas.util._test_decorators as td
78

89
from pandas.core.dtypes.base import _registry as ea_registry
@@ -1400,7 +1401,7 @@ def test_frame_setitem_empty_dataframe(self):
14001401
)
14011402
tm.assert_frame_equal(df, expected)
14021403

1403-
def test_iloc_setitem_view_2dblock(self):
1404+
def test_iloc_setitem_view_2dblock(self, using_copy_on_write, warn_copy_on_write):
14041405
# https://github.com/pandas-dev/pandas/issues/60309
14051406
df_parent = DataFrame(
14061407
{
@@ -1414,19 +1415,34 @@ def test_iloc_setitem_view_2dblock(self):
14141415
df = df_parent[["B", "C"]]
14151416

14161417
# Perform the iloc operation
1417-
df.iloc[[1, 3], :] = [[2, 2], [2, 2]]
1418-
1419-
# Check that original DataFrame is unchanged
1420-
tm.assert_frame_equal(df_parent, df_orig)
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]]
14211431

14221432
# Check that df is modified correctly
14231433
expected = DataFrame({"B": [2, 2, 2, 2], "C": [3, 2, 1, 2]}, index=df.index)
14241434
tm.assert_frame_equal(df, expected)
14251435

14261436
# with setting to subset of columns
14271437
df = df_parent[["B", "C", "D"]]
1428-
df.iloc[[1, 3], 0:3:2] = [[2, 2], [2, 2]]
1429-
tm.assert_frame_equal(df_parent, df_orig)
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+
14301446
expected = DataFrame(
14311447
{"B": [2, 2, 2, 2], "C": [3, 6, 1, 7], "D": [8, 2, 10, 2]}, index=df.index
14321448
)
@@ -1448,7 +1464,9 @@ def test_iloc_setitem_view_2dblock(self):
14481464
),
14491465
],
14501466
)
1451-
def test_setitem_2dblock_with_ref(self, indexer, value):
1467+
def test_setitem_2dblock_with_ref(
1468+
self, indexer, value, using_copy_on_write, warn_copy_on_write
1469+
):
14521470
# https://github.com/pandas-dev/pandas/issues/60309
14531471
arr = np.arange(12).reshape(3, 4)
14541472

@@ -1459,10 +1477,12 @@ def test_setitem_2dblock_with_ref(self, indexer, value):
14591477
df_orig = df_parent.copy()
14601478
df = df_parent[:]
14611479

1462-
df.iloc[indexer] = value
1480+
with tm.assert_cow_warning(warn_copy_on_write):
1481+
df.iloc[indexer] = value
14631482

14641483
# Check that original DataFrame is unchanged
1465-
tm.assert_frame_equal(df_parent, df_orig)
1484+
if using_copy_on_write:
1485+
tm.assert_frame_equal(df_parent, df_orig)
14661486

14671487
# Check that df is modified correctly
14681488
arr[indexer] = value

pandas/tests/indexing/test_iloc.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def test_iloc_setitem_fullcol_categorical(self, indexer, key, using_array_manage
104104
expected = DataFrame({0: Series(cat.astype(object), dtype=object), 1: range(3)})
105105
tm.assert_frame_equal(df, expected)
106106

107+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
107108
@pytest.mark.parametrize("has_ref", [True, False])
108109
@pytest.mark.parametrize("box", [array, Series])
109110
def test_iloc_setitem_ea_inplace(
@@ -432,6 +433,7 @@ def test_iloc_getitem_slice_dups(self):
432433
tm.assert_frame_equal(df.iloc[10:, :2], df2)
433434
tm.assert_frame_equal(df.iloc[10:, 2:], df1)
434435

436+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
435437
@pytest.mark.parametrize("has_ref", [True, False])
436438
def test_iloc_setitem(self, warn_copy_on_write, has_ref):
437439
df = DataFrame(
@@ -457,6 +459,7 @@ def test_iloc_setitem(self, warn_copy_on_write, has_ref):
457459
expected = Series([0, 1, 0], index=[4, 5, 6])
458460
tm.assert_series_equal(s, expected)
459461

462+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
460463
@pytest.mark.parametrize("has_ref", [True, False])
461464
def test_iloc_setitem_axis_argument(self, has_ref):
462465
# GH45032
@@ -478,6 +481,7 @@ def test_iloc_setitem_axis_argument(self, has_ref):
478481
df.iloc(axis=1)[2] = 5
479482
tm.assert_frame_equal(df, expected)
480483

484+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
481485
@pytest.mark.parametrize("has_ref", [True, False])
482486
def test_iloc_setitem_list(self, has_ref):
483487
# setitem with an iloc list
@@ -686,6 +690,7 @@ def test_iloc_getitem_doc_issue(self, using_array_manager):
686690
expected = DataFrame(arr[1:5, 2:4], index=index[1:5], columns=columns[2:4])
687691
tm.assert_frame_equal(result, expected)
688692

693+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
689694
@pytest.mark.parametrize("has_ref", [True, False])
690695
def test_iloc_setitem_series(self, has_ref):
691696
df = DataFrame(
@@ -723,6 +728,7 @@ def test_iloc_setitem_series(self, has_ref):
723728
expected = Series([0, 1, 2, 3, 4, 5])
724729
tm.assert_series_equal(result, expected)
725730

731+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
726732
@pytest.mark.parametrize("has_ref", [True, False])
727733
def test_iloc_setitem_list_of_lists(self, has_ref):
728734
# GH 7551
@@ -745,6 +751,7 @@ def test_iloc_setitem_list_of_lists(self, has_ref):
745751
expected = DataFrame({"A": ["a", "b", "x", "y", "e"], "B": [5, 6, 11, 13, 9]})
746752
tm.assert_frame_equal(df, expected)
747753

754+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
748755
@pytest.mark.parametrize("has_ref", [True, False])
749756
@pytest.mark.parametrize("indexer", [[0], slice(None, 1, None), np.array([0])])
750757
@pytest.mark.parametrize("value", [["Z"], np.array(["Z"])])
@@ -1062,6 +1069,7 @@ def test_iloc_setitem_bool_indexer(self, klass):
10621069
expected = DataFrame({"flag": ["x", "y", "z"], "value": [2, 3, 4]})
10631070
tm.assert_frame_equal(df, expected)
10641071

1072+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
10651073
@pytest.mark.parametrize("has_ref", [True, False])
10661074
@pytest.mark.parametrize("indexer", [[1], slice(1, 2)])
10671075
def test_iloc_setitem_pure_position_based(self, indexer, has_ref):
@@ -1074,6 +1082,7 @@ def test_iloc_setitem_pure_position_based(self, indexer, has_ref):
10741082
expected = DataFrame({"a": [1, 2, 3], "b": [11, 12, 13], "c": [7, 8, 9]})
10751083
tm.assert_frame_equal(df2, expected)
10761084

1085+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
10771086
@pytest.mark.parametrize("has_ref", [True, False])
10781087
def test_iloc_setitem_dictionary_value(self, has_ref):
10791088
# GH#37728
@@ -1302,6 +1311,7 @@ def test_iloc_float_raises(
13021311
):
13031312
obj.iloc[3.0] = 0
13041313

1314+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
13051315
@pytest.mark.parametrize("has_ref", [True, False])
13061316
def test_iloc_getitem_setitem_fancy_exceptions(self, float_frame, has_ref):
13071317
with pytest.raises(IndexingError, match="Too many indexers"):

pandas/tests/indexing/test_loc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ def test_loc_modify_datetime(self):
755755

756756
tm.assert_frame_equal(df, expected)
757757

758+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
758759
@pytest.mark.parametrize("has_ref", [True, False])
759760
def test_loc_setitem_frame_with_reindex(self, has_ref):
760761
# GH#6254 setting issue
@@ -791,6 +792,7 @@ def test_loc_setitem_frame_with_inverted_slice(self):
791792
expected = DataFrame({"A": [3.0, 2.0, 1.0], "B": "string"}, index=[1, 2, 3])
792793
tm.assert_frame_equal(df, expected)
793794

795+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
794796
@pytest.mark.parametrize("has_ref", [True, False])
795797
def test_loc_setitem_empty_frame(self, has_ref):
796798
# GH#6252 setting with an empty frame
@@ -819,6 +821,7 @@ def test_loc_setitem_empty_frame(self, has_ref):
819821
)
820822
tm.assert_frame_equal(df, expected)
821823

824+
@pytest.mark.filterwarnings("ignore:Setting a value on a view:FutureWarning")
822825
@pytest.mark.parametrize("has_ref", [True, False])
823826
def test_loc_setitem_frame(self, has_ref):
824827
df = DataFrame(

0 commit comments

Comments
 (0)