@@ -104,12 +104,17 @@ 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 .parametrize ("has_ref" , [True , False ])
107108 @pytest .mark .parametrize ("box" , [array , Series ])
108- def test_iloc_setitem_ea_inplace (self , frame_or_series , box , using_copy_on_write ):
109+ def test_iloc_setitem_ea_inplace (
110+ self , frame_or_series , box , has_ref , using_copy_on_write
111+ ):
109112 # GH#38952 Case with not setting a full column
110113 # IntegerArray without NAs
111114 arr = array ([1 , 2 , 3 , 4 ])
112115 obj = frame_or_series (arr .to_numpy ("i8" ))
116+ if has_ref :
117+ view = obj [:] # noqa: F841
113118
114119 if frame_or_series is Series :
115120 values = obj .values
@@ -125,14 +130,15 @@ def test_iloc_setitem_ea_inplace(self, frame_or_series, box, using_copy_on_write
125130 tm .assert_equal (obj , expected )
126131
127132 # Check that we are actually in-place
128- if frame_or_series is Series :
129- if using_copy_on_write :
130- assert obj .values is not values
131- assert np .shares_memory (obj .values , values )
133+ if not has_ref :
134+ if frame_or_series is Series :
135+ if using_copy_on_write :
136+ assert obj .values is not values
137+ assert np .shares_memory (obj .values , values )
138+ else :
139+ assert obj .values is values
132140 else :
133- assert obj .values is values
134- else :
135- assert np .shares_memory (obj [0 ].values , values )
141+ assert np .shares_memory (obj [0 ].values , values )
136142
137143 def test_is_scalar_access (self ):
138144 # GH#32085 index with duplicates doesn't matter for _is_scalar_access
@@ -426,12 +432,15 @@ def test_iloc_getitem_slice_dups(self):
426432 tm .assert_frame_equal (df .iloc [10 :, :2 ], df2 )
427433 tm .assert_frame_equal (df .iloc [10 :, 2 :], df1 )
428434
429- def test_iloc_setitem (self , warn_copy_on_write ):
435+ @pytest .mark .parametrize ("has_ref" , [True , False ])
436+ def test_iloc_setitem (self , warn_copy_on_write , has_ref ):
430437 df = DataFrame (
431438 np .random .default_rng (2 ).standard_normal ((4 , 4 )),
432439 index = np .arange (0 , 8 , 2 ),
433440 columns = np .arange (0 , 12 , 3 ),
434441 )
442+ if has_ref :
443+ view = df [:] # noqa: F841
435444
436445 df .iloc [1 , 1 ] = 1
437446 result = df .iloc [1 , 1 ]
@@ -448,27 +457,35 @@ def test_iloc_setitem(self, warn_copy_on_write):
448457 expected = Series ([0 , 1 , 0 ], index = [4 , 5 , 6 ])
449458 tm .assert_series_equal (s , expected )
450459
451- def test_iloc_setitem_axis_argument (self ):
460+ @pytest .mark .parametrize ("has_ref" , [True , False ])
461+ def test_iloc_setitem_axis_argument (self , has_ref ):
452462 # GH45032
453463 df = DataFrame ([[6 , "c" , 10 ], [7 , "d" , 11 ], [8 , "e" , 12 ]])
454464 df [1 ] = df [1 ].astype (object )
465+ if has_ref :
466+ view = df [:]
455467 expected = DataFrame ([[6 , "c" , 10 ], [7 , "d" , 11 ], [5 , 5 , 5 ]])
456468 expected [1 ] = expected [1 ].astype (object )
457469 df .iloc (axis = 0 )[2 ] = 5
458470 tm .assert_frame_equal (df , expected )
459471
460472 df = DataFrame ([[6 , "c" , 10 ], [7 , "d" , 11 ], [8 , "e" , 12 ]])
461473 df [1 ] = df [1 ].astype (object )
474+ if has_ref :
475+ view = df [:] # noqa: F841
462476 expected = DataFrame ([[6 , "c" , 5 ], [7 , "d" , 5 ], [8 , "e" , 5 ]])
463477 expected [1 ] = expected [1 ].astype (object )
464478 df .iloc (axis = 1 )[2 ] = 5
465479 tm .assert_frame_equal (df , expected )
466480
467- def test_iloc_setitem_list (self ):
481+ @pytest .mark .parametrize ("has_ref" , [True , False ])
482+ def test_iloc_setitem_list (self , has_ref ):
468483 # setitem with an iloc list
469484 df = DataFrame (
470485 np .arange (9 ).reshape ((3 , 3 )), index = ["A" , "B" , "C" ], columns = ["A" , "B" , "C" ]
471486 )
487+ if has_ref :
488+ view = df [:] # noqa: F841
472489 df .iloc [[0 , 1 ], [1 , 2 ]]
473490 df .iloc [[0 , 1 ], [1 , 2 ]] += 100
474491
@@ -669,12 +686,15 @@ def test_iloc_getitem_doc_issue(self, using_array_manager):
669686 expected = DataFrame (arr [1 :5 , 2 :4 ], index = index [1 :5 ], columns = columns [2 :4 ])
670687 tm .assert_frame_equal (result , expected )
671688
672- def test_iloc_setitem_series (self ):
689+ @pytest .mark .parametrize ("has_ref" , [True , False ])
690+ def test_iloc_setitem_series (self , has_ref ):
673691 df = DataFrame (
674692 np .random .default_rng (2 ).standard_normal ((10 , 4 )),
675693 index = list ("abcdefghij" ),
676694 columns = list ("ABCD" ),
677695 )
696+ if has_ref :
697+ view = df [:] # noqa: F841
678698
679699 df .iloc [1 , 1 ] = 1
680700 result = df .iloc [1 , 1 ]
@@ -703,32 +723,40 @@ def test_iloc_setitem_series(self):
703723 expected = Series ([0 , 1 , 2 , 3 , 4 , 5 ])
704724 tm .assert_series_equal (result , expected )
705725
706- def test_iloc_setitem_list_of_lists (self ):
726+ @pytest .mark .parametrize ("has_ref" , [True , False ])
727+ def test_iloc_setitem_list_of_lists (self , has_ref ):
707728 # GH 7551
708729 # list-of-list is set incorrectly in mixed vs. single dtyped frames
709730 df = DataFrame (
710731 {"A" : np .arange (5 , dtype = "int64" ), "B" : np .arange (5 , 10 , dtype = "int64" )}
711732 )
733+ if has_ref :
734+ view = df [:]
712735 df .iloc [2 :4 ] = [[10 , 11 ], [12 , 13 ]]
713736 expected = DataFrame ({"A" : [0 , 1 , 10 , 12 , 4 ], "B" : [5 , 6 , 11 , 13 , 9 ]})
714737 tm .assert_frame_equal (df , expected )
715738
716739 df = DataFrame (
717740 {"A" : ["a" , "b" , "c" , "d" , "e" ], "B" : np .arange (5 , 10 , dtype = "int64" )}
718741 )
742+ if has_ref :
743+ view = df [:] # noqa: F841
719744 df .iloc [2 :4 ] = [["x" , 11 ], ["y" , 13 ]]
720745 expected = DataFrame ({"A" : ["a" , "b" , "x" , "y" , "e" ], "B" : [5 , 6 , 11 , 13 , 9 ]})
721746 tm .assert_frame_equal (df , expected )
722747
748+ @pytest .mark .parametrize ("has_ref" , [True , False ])
723749 @pytest .mark .parametrize ("indexer" , [[0 ], slice (None , 1 , None ), np .array ([0 ])])
724750 @pytest .mark .parametrize ("value" , [["Z" ], np .array (["Z" ])])
725- def test_iloc_setitem_with_scalar_index (self , indexer , value ):
751+ def test_iloc_setitem_with_scalar_index (self , has_ref , indexer , value ):
726752 # GH #19474
727753 # assigning like "df.iloc[0, [0]] = ['Z']" should be evaluated
728754 # elementwisely, not using "setter('A', ['Z'])".
729755
730756 # Set object type to avoid upcast when setting "Z"
731757 df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ["A" , "B" ]).astype ({"A" : object })
758+ if has_ref :
759+ view = df [:] # noqa: F841
732760 df .iloc [0 , indexer ] = value
733761 result = df .iloc [0 , 0 ]
734762
@@ -1034,25 +1062,33 @@ def test_iloc_setitem_bool_indexer(self, klass):
10341062 expected = DataFrame ({"flag" : ["x" , "y" , "z" ], "value" : [2 , 3 , 4 ]})
10351063 tm .assert_frame_equal (df , expected )
10361064
1065+ @pytest .mark .parametrize ("has_ref" , [True , False ])
10371066 @pytest .mark .parametrize ("indexer" , [[1 ], slice (1 , 2 )])
1038- def test_iloc_setitem_pure_position_based (self , indexer ):
1067+ def test_iloc_setitem_pure_position_based (self , indexer , has_ref ):
10391068 # GH#22046
10401069 df1 = DataFrame ({"a2" : [11 , 12 , 13 ], "b2" : [14 , 15 , 16 ]})
10411070 df2 = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [7 , 8 , 9 ]})
1071+ if has_ref :
1072+ view = df2 [:] # noqa: F841
10421073 df2 .iloc [:, indexer ] = df1 .iloc [:, [0 ]]
10431074 expected = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [11 , 12 , 13 ], "c" : [7 , 8 , 9 ]})
10441075 tm .assert_frame_equal (df2 , expected )
10451076
1046- def test_iloc_setitem_dictionary_value (self ):
1077+ @pytest .mark .parametrize ("has_ref" , [True , False ])
1078+ def test_iloc_setitem_dictionary_value (self , has_ref ):
10471079 # GH#37728
10481080 df = DataFrame ({"x" : [1 , 2 ], "y" : [2 , 2 ]})
1081+ if has_ref :
1082+ view = df [:]
10491083 rhs = {"x" : 9 , "y" : 99 }
10501084 df .iloc [1 ] = rhs
10511085 expected = DataFrame ({"x" : [1 , 9 ], "y" : [2 , 99 ]})
10521086 tm .assert_frame_equal (df , expected )
10531087
10541088 # GH#38335 same thing, mixed dtypes
10551089 df = DataFrame ({"x" : [1 , 2 ], "y" : [2.0 , 2.0 ]})
1090+ if has_ref :
1091+ view = df [:] # noqa: F841
10561092 df .iloc [1 ] = rhs
10571093 expected = DataFrame ({"x" : [1 , 9 ], "y" : [2.0 , 99.0 ]})
10581094 tm .assert_frame_equal (df , expected )
@@ -1266,10 +1302,13 @@ def test_iloc_float_raises(
12661302 ):
12671303 obj .iloc [3.0 ] = 0
12681304
1269- def test_iloc_getitem_setitem_fancy_exceptions (self , float_frame ):
1305+ @pytest .mark .parametrize ("has_ref" , [True , False ])
1306+ def test_iloc_getitem_setitem_fancy_exceptions (self , float_frame , has_ref ):
12701307 with pytest .raises (IndexingError , match = "Too many indexers" ):
12711308 float_frame .iloc [:, :, :]
12721309
1310+ if has_ref :
1311+ view = float_frame [:] # noqa: F841
12731312 with pytest .raises (IndexError , match = "too many indices for array" ):
12741313 # GH#32257 we let numpy do validation, get their exception
12751314 float_frame .iloc [:, :, :] = 1
0 commit comments