@@ -93,6 +93,90 @@ def test_setitem_iloc_scalar_multiple_homogoneous(self, data):
93
93
df .iloc [10 , 1 ] = data [1 ]
94
94
assert df .loc [10 , "B" ] == data [1 ]
95
95
96
+ @pytest .mark .parametrize (
97
+ "mask" ,
98
+ [
99
+ np .array ([True , True , True , False , False ]),
100
+ pd .array ([True , True , True , False , False ], dtype = "boolean" ),
101
+ pd .array ([True , True , True , pd .NA , pd .NA ], dtype = "boolean" ),
102
+ ],
103
+ ids = ["numpy-array" , "boolean-array" , "boolean-array-na" ],
104
+ )
105
+ def test_setitem_mask (self , data , mask , box_in_series ):
106
+ arr = data [:5 ].copy ()
107
+ expected = arr .take ([0 , 0 , 0 , 3 , 4 ])
108
+ if box_in_series :
109
+ arr = pd .Series (arr )
110
+ expected = pd .Series (expected )
111
+ arr [mask ] = data [0 ]
112
+ self .assert_equal (expected , arr )
113
+
114
+ def test_setitem_mask_raises (self , data , box_in_series ):
115
+ # wrong length
116
+ mask = np .array ([True , False ])
117
+
118
+ if box_in_series :
119
+ data = pd .Series (data )
120
+
121
+ with pytest .raises (IndexError , match = "wrong length" ):
122
+ data [mask ] = data [0 ]
123
+
124
+ mask = pd .array (mask , dtype = "boolean" )
125
+ with pytest .raises (IndexError , match = "wrong length" ):
126
+ data [mask ] = data [0 ]
127
+
128
+ def test_setitem_mask_boolean_array_with_na (self , data , box_in_series ):
129
+ mask = pd .array (np .zeros (data .shape , dtype = "bool" ), dtype = "boolean" )
130
+ mask [:3 ] = True
131
+ mask [3 :5 ] = pd .NA
132
+
133
+ if box_in_series :
134
+ data = pd .Series (data )
135
+
136
+ data [mask ] = data [0 ]
137
+
138
+ assert (data [:3 ] == data [0 ]).all ()
139
+
140
+ @pytest .mark .parametrize (
141
+ "idx" ,
142
+ [[0 , 1 , 2 ], pd .array ([0 , 1 , 2 ], dtype = "Int64" ), np .array ([0 , 1 , 2 ])],
143
+ ids = ["list" , "integer-array" , "numpy-array" ],
144
+ )
145
+ def test_setitem_integer_array (self , data , idx , box_in_series ):
146
+ arr = data [:5 ].copy ()
147
+ expected = data .take ([0 , 0 , 0 , 3 , 4 ])
148
+
149
+ if box_in_series :
150
+ arr = pd .Series (arr )
151
+ expected = pd .Series (expected )
152
+
153
+ arr [idx ] = arr [0 ]
154
+ self .assert_equal (arr , expected )
155
+
156
+ @pytest .mark .parametrize (
157
+ "idx, box_in_series" ,
158
+ [
159
+ ([0 , 1 , 2 , pd .NA ], False ),
160
+ pytest .param (
161
+ [0 , 1 , 2 , pd .NA ], True , marks = pytest .mark .xfail (reason = "GH-31948" )
162
+ ),
163
+ (pd .array ([0 , 1 , 2 , pd .NA ], dtype = "Int64" ), False ),
164
+ (pd .array ([0 , 1 , 2 , pd .NA ], dtype = "Int64" ), False ),
165
+ ],
166
+ ids = ["list-False" , "list-True" , "integer-array-False" , "integer-array-True" ],
167
+ )
168
+ def test_setitem_integer_with_missing_raises (self , data , idx , box_in_series ):
169
+ arr = data .copy ()
170
+
171
+ # TODO(xfail) this raises KeyError about labels not found (it tries label-based)
172
+ # for list of labels with Series
173
+ if box_in_series :
174
+ arr = pd .Series (data , index = [tm .rands (4 ) for _ in range (len (data ))])
175
+
176
+ msg = "Cannot index with an integer indexer containing NA values"
177
+ with pytest .raises (ValueError , match = msg ):
178
+ arr [idx ] = arr [0 ]
179
+
96
180
@pytest .mark .parametrize ("as_callable" , [True , False ])
97
181
@pytest .mark .parametrize ("setter" , ["loc" , None ])
98
182
def test_setitem_mask_aligned (self , data , as_callable , setter ):
0 commit comments