@@ -215,6 +215,63 @@ def test_setitem_list_of_tuples(self, float_frame):
215
215
expected = Series (tuples , index = float_frame .index , name = "tuples" )
216
216
tm .assert_series_equal (result , expected )
217
217
218
+ @pytest .mark .parametrize (
219
+ "columns,box,expected" ,
220
+ [
221
+ (
222
+ ["A" , "B" , "C" , "D" ],
223
+ 7 ,
224
+ pd .DataFrame (
225
+ [[7 , 7 , 7 , 7 ], [7 , 7 , 7 , 7 ], [7 , 7 , 7 , 7 ]],
226
+ columns = ["A" , "B" , "C" , "D" ],
227
+ ),
228
+ ),
229
+ (
230
+ ["C" , "D" ],
231
+ [7 , 8 ],
232
+ pd .DataFrame (
233
+ [[1 , 2 , 7 , 8 ], [3 , 4 , 7 , 8 ], [5 , 6 , 7 , 8 ]],
234
+ columns = ["A" , "B" , "C" , "D" ],
235
+ ),
236
+ ),
237
+ (
238
+ ["A" , "B" , "C" ],
239
+ np .array ([7 , 8 , 9 ], dtype = np .int64 ),
240
+ pd .DataFrame (
241
+ [[7 , 8 , 9 ], [7 , 8 , 9 ], [7 , 8 , 9 ]], columns = ["A" , "B" , "C" ]
242
+ ),
243
+ ),
244
+ (
245
+ ["B" , "C" , "D" ],
246
+ [[7 , 8 , 9 ], [10 , 11 , 12 ], [13 , 14 , 15 ]],
247
+ pd .DataFrame (
248
+ [[1 , 7 , 8 , 9 ], [3 , 10 , 11 , 12 ], [5 , 13 , 14 , 15 ]],
249
+ columns = ["A" , "B" , "C" , "D" ],
250
+ ),
251
+ ),
252
+ (
253
+ ["C" , "A" , "D" ],
254
+ np .array ([[7 , 8 , 9 ], [10 , 11 , 12 ], [13 , 14 , 15 ]], dtype = np .int64 ),
255
+ pd .DataFrame (
256
+ [[8 , 2 , 7 , 9 ], [11 , 4 , 10 , 12 ], [14 , 6 , 13 , 15 ]],
257
+ columns = ["A" , "B" , "C" , "D" ],
258
+ ),
259
+ ),
260
+ (
261
+ ["A" , "C" ],
262
+ pd .DataFrame ([[7 , 8 ], [9 , 10 ], [11 , 12 ]], columns = ["A" , "C" ]),
263
+ pd .DataFrame (
264
+ [[7 , 2 , 8 ], [9 , 4 , 10 ], [11 , 6 , 12 ]], columns = ["A" , "B" , "C" ]
265
+ ),
266
+ ),
267
+ ],
268
+ )
269
+ def test_setitem_list_missing_columns (self , columns , box , expected ):
270
+ # GH 29334
271
+ df = pd .DataFrame ([[1 , 2 ], [3 , 4 ], [5 , 6 ]], columns = ["A" , "B" ])
272
+ df [columns ] = box
273
+ tm .assert_frame_equal (df , expected )
274
+
218
275
def test_setitem_multi_index (self ):
219
276
# GH7655, test that assigning to a sub-frame of a frame
220
277
# with multi-index columns aligns both rows and columns
@@ -459,13 +516,6 @@ def test_setitem(self, float_frame):
459
516
float_frame ["col6" ] = series
460
517
tm .assert_series_equal (series , float_frame ["col6" ], check_names = False )
461
518
462
- msg = (
463
- r"\"None of \[Float64Index\(\[.*dtype='float64'\)\] are in the "
464
- r"\[columns\]\""
465
- )
466
- with pytest .raises (KeyError , match = msg ):
467
- float_frame [np .random .randn (len (float_frame ) + 1 )] = 1
468
-
469
519
# set ndarray
470
520
arr = np .random .randn (len (float_frame ))
471
521
float_frame ["col9" ] = arr
0 commit comments