@@ -105,11 +105,16 @@ def test_iloc_setitem_fullcol_categorical(self, indexer_li, key):
105
105
expected = DataFrame ({0 : Series (cat .astype (object ), dtype = object ), 1 : range (3 )})
106
106
tm .assert_frame_equal (df , expected )
107
107
108
- def test_iloc_setitem_ea_inplace (self , frame_or_series , index_or_series_or_array ):
108
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
109
+ def test_iloc_setitem_ea_inplace (
110
+ self , frame_or_series , index_or_series_or_array , has_ref
111
+ ):
109
112
# GH#38952 Case with not setting a full column
110
113
# IntegerArray without NAs
111
114
arr = array ([1 , 2 , 3 , 4 ])
112
115
obj = frame_or_series (arr .to_numpy ("i8" ))
116
+ if has_ref :
117
+ view = obj [:] # noqa: F841
113
118
114
119
if frame_or_series is Series :
115
120
values = obj .values
@@ -125,11 +130,12 @@ def test_iloc_setitem_ea_inplace(self, frame_or_series, index_or_series_or_array
125
130
tm .assert_equal (obj , expected )
126
131
127
132
# Check that we are actually in-place
128
- if frame_or_series is Series :
129
- assert obj .values is not values
130
- assert np .shares_memory (obj .values , values )
131
- else :
132
- assert np .shares_memory (obj [0 ].values , values )
133
+ if not has_ref :
134
+ if frame_or_series is Series :
135
+ assert obj .values is not values
136
+ assert np .shares_memory (obj .values , values )
137
+ else :
138
+ assert np .shares_memory (obj [0 ].values , values )
133
139
134
140
def test_is_scalar_access (self ):
135
141
# GH#32085 index with duplicates doesn't matter for _is_scalar_access
@@ -426,12 +432,15 @@ def test_iloc_getitem_slice_dups(self):
426
432
tm .assert_frame_equal (df .iloc [10 :, :2 ], df2 )
427
433
tm .assert_frame_equal (df .iloc [10 :, 2 :], df1 )
428
434
429
- def test_iloc_setitem (self ):
435
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
436
+ def test_iloc_setitem (sel , has_ref ):
430
437
df = DataFrame (
431
438
np .random .default_rng (2 ).standard_normal ((4 , 4 )),
432
439
index = np .arange (0 , 8 , 2 ),
433
440
columns = np .arange (0 , 12 , 3 ),
434
441
)
442
+ if has_ref :
443
+ view = df [:] # noqa: F841
435
444
436
445
df .iloc [1 , 1 ] = 1
437
446
result = df .iloc [1 , 1 ]
@@ -448,27 +457,35 @@ def test_iloc_setitem(self):
448
457
expected = Series ([0 , 1 , 0 ], index = [4 , 5 , 6 ])
449
458
tm .assert_series_equal (s , expected )
450
459
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 ):
452
462
# GH45032
453
463
df = DataFrame ([[6 , "c" , 10 ], [7 , "d" , 11 ], [8 , "e" , 12 ]])
454
464
df [1 ] = df [1 ].astype (object )
465
+ if has_ref :
466
+ view = df [:]
455
467
expected = DataFrame ([[6 , "c" , 10 ], [7 , "d" , 11 ], [5 , 5 , 5 ]])
456
468
expected [1 ] = expected [1 ].astype (object )
457
469
df .iloc (axis = 0 )[2 ] = 5
458
470
tm .assert_frame_equal (df , expected )
459
471
460
472
df = DataFrame ([[6 , "c" , 10 ], [7 , "d" , 11 ], [8 , "e" , 12 ]])
461
473
df [1 ] = df [1 ].astype (object )
474
+ if has_ref :
475
+ view = df [:] # noqa: F841
462
476
expected = DataFrame ([[6 , "c" , 5 ], [7 , "d" , 5 ], [8 , "e" , 5 ]])
463
477
expected [1 ] = expected [1 ].astype (object )
464
478
df .iloc (axis = 1 )[2 ] = 5
465
479
tm .assert_frame_equal (df , expected )
466
480
467
- def test_iloc_setitem_list (self ):
481
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
482
+ def test_iloc_setitem_list (self , has_ref ):
468
483
# setitem with an iloc list
469
484
df = DataFrame (
470
485
np .arange (9 ).reshape ((3 , 3 )), index = ["A" , "B" , "C" ], columns = ["A" , "B" , "C" ]
471
486
)
487
+ if has_ref :
488
+ view = df [:] # noqa: F841
472
489
df .iloc [[0 , 1 ], [1 , 2 ]]
473
490
df .iloc [[0 , 1 ], [1 , 2 ]] += 100
474
491
@@ -663,12 +680,15 @@ def test_iloc_getitem_doc_issue(self):
663
680
expected = DataFrame (arr [1 :5 , 2 :4 ], index = index [1 :5 ], columns = columns [2 :4 ])
664
681
tm .assert_frame_equal (result , expected )
665
682
666
- def test_iloc_setitem_series (self ):
683
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
684
+ def test_iloc_setitem_series (self , has_ref ):
667
685
df = DataFrame (
668
686
np .random .default_rng (2 ).standard_normal ((10 , 4 )),
669
687
index = list ("abcdefghij" ),
670
688
columns = list ("ABCD" ),
671
689
)
690
+ if has_ref :
691
+ view = df [:] # noqa: F841
672
692
673
693
df .iloc [1 , 1 ] = 1
674
694
result = df .iloc [1 , 1 ]
@@ -697,32 +717,40 @@ def test_iloc_setitem_series(self):
697
717
expected = Series ([0 , 1 , 2 , 3 , 4 , 5 ])
698
718
tm .assert_series_equal (result , expected )
699
719
700
- def test_iloc_setitem_list_of_lists (self ):
720
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
721
+ def test_iloc_setitem_list_of_lists (self , has_ref ):
701
722
# GH 7551
702
723
# list-of-list is set incorrectly in mixed vs. single dtyped frames
703
724
df = DataFrame (
704
725
{"A" : np .arange (5 , dtype = "int64" ), "B" : np .arange (5 , 10 , dtype = "int64" )}
705
726
)
727
+ if has_ref :
728
+ view = df [:]
706
729
df .iloc [2 :4 ] = [[10 , 11 ], [12 , 13 ]]
707
730
expected = DataFrame ({"A" : [0 , 1 , 10 , 12 , 4 ], "B" : [5 , 6 , 11 , 13 , 9 ]})
708
731
tm .assert_frame_equal (df , expected )
709
732
710
733
df = DataFrame (
711
734
{"A" : ["a" , "b" , "c" , "d" , "e" ], "B" : np .arange (5 , 10 , dtype = "int64" )}
712
735
)
736
+ if has_ref :
737
+ view = df [:] # noqa: F841
713
738
df .iloc [2 :4 ] = [["x" , 11 ], ["y" , 13 ]]
714
739
expected = DataFrame ({"A" : ["a" , "b" , "x" , "y" , "e" ], "B" : [5 , 6 , 11 , 13 , 9 ]})
715
740
tm .assert_frame_equal (df , expected )
716
741
742
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
717
743
@pytest .mark .parametrize ("indexer" , [[0 ], slice (None , 1 , None ), np .array ([0 ])])
718
744
@pytest .mark .parametrize ("value" , [["Z" ], np .array (["Z" ])])
719
- def test_iloc_setitem_with_scalar_index (self , indexer , value ):
745
+ def test_iloc_setitem_with_scalar_index (self , has_ref , indexer , value ):
720
746
# GH #19474
721
747
# assigning like "df.iloc[0, [0]] = ['Z']" should be evaluated
722
748
# elementwisely, not using "setter('A', ['Z'])".
723
749
724
750
# Set object type to avoid upcast when setting "Z"
725
751
df = DataFrame ([[1 , 2 ], [3 , 4 ]], columns = ["A" , "B" ]).astype ({"A" : object })
752
+ if has_ref :
753
+ view = df [:] # noqa: F841
726
754
df .iloc [0 , indexer ] = value
727
755
result = df .iloc [0 , 0 ]
728
756
@@ -1048,25 +1076,33 @@ def test_iloc_setitem_bool_indexer(self, klass):
1048
1076
expected = DataFrame ({"flag" : ["x" , "y" , "z" ], "value" : [2 , 3 , 4 ]})
1049
1077
tm .assert_frame_equal (df , expected )
1050
1078
1079
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
1051
1080
@pytest .mark .parametrize ("indexer" , [[1 ], slice (1 , 2 )])
1052
- def test_iloc_setitem_pure_position_based (self , indexer ):
1081
+ def test_iloc_setitem_pure_position_based (self , indexer , has_ref ):
1053
1082
# GH#22046
1054
1083
df1 = DataFrame ({"a2" : [11 , 12 , 13 ], "b2" : [14 , 15 , 16 ]})
1055
1084
df2 = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ], "c" : [7 , 8 , 9 ]})
1085
+ if has_ref :
1086
+ view = df2 [:] # noqa: F841
1056
1087
df2 .iloc [:, indexer ] = df1 .iloc [:, [0 ]]
1057
1088
expected = DataFrame ({"a" : [1 , 2 , 3 ], "b" : [11 , 12 , 13 ], "c" : [7 , 8 , 9 ]})
1058
1089
tm .assert_frame_equal (df2 , expected )
1059
1090
1060
- def test_iloc_setitem_dictionary_value (self ):
1091
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
1092
+ def test_iloc_setitem_dictionary_value (self , has_ref ):
1061
1093
# GH#37728
1062
1094
df = DataFrame ({"x" : [1 , 2 ], "y" : [2 , 2 ]})
1095
+ if has_ref :
1096
+ view = df [:]
1063
1097
rhs = {"x" : 9 , "y" : 99 }
1064
1098
df .iloc [1 ] = rhs
1065
1099
expected = DataFrame ({"x" : [1 , 9 ], "y" : [2 , 99 ]})
1066
1100
tm .assert_frame_equal (df , expected )
1067
1101
1068
1102
# GH#38335 same thing, mixed dtypes
1069
1103
df = DataFrame ({"x" : [1 , 2 ], "y" : [2.0 , 2.0 ]})
1104
+ if has_ref :
1105
+ view = df [:] # noqa: F841
1070
1106
df .iloc [1 ] = rhs
1071
1107
expected = DataFrame ({"x" : [1 , 9 ], "y" : [2.0 , 99.0 ]})
1072
1108
tm .assert_frame_equal (df , expected )
@@ -1272,10 +1308,13 @@ def test_iloc_float_raises(self, series_with_simple_index, frame_or_series):
1272
1308
with pytest .raises (IndexError , match = _slice_iloc_msg ):
1273
1309
obj .iloc [3.0 ] = 0
1274
1310
1275
- def test_iloc_getitem_setitem_fancy_exceptions (self , float_frame ):
1311
+ @pytest .mark .parametrize ("has_ref" , [True , False ])
1312
+ def test_iloc_getitem_setitem_fancy_exceptions (self , float_frame , has_ref ):
1276
1313
with pytest .raises (IndexingError , match = "Too many indexers" ):
1277
1314
float_frame .iloc [:, :, :]
1278
1315
1316
+ if has_ref :
1317
+ view = float_frame [:] # noqa: F841
1279
1318
with pytest .raises (IndexError , match = "too many indices for array" ):
1280
1319
# GH#32257 we let numpy do validation, get their exception
1281
1320
float_frame .iloc [:, :, :] = 1
0 commit comments