@@ -208,6 +208,47 @@ def test_setitem_list_of_tuples(self, float_frame):
208208 expected = Series (tuples , index = float_frame .index , name = "tuples" )
209209 assert_series_equal (result , expected )
210210
211+ def test_setitem_list_all_missing_columns_scalar (self , float_frame ):
212+ # GH 26534
213+ result = float_frame .copy ()
214+ result [["E" , "F" ]] = 1
215+ expected = float_frame .copy ()
216+ # force the dtypes to be float as currently multcolumn assignment does not
217+ # change column dtype from float to int even when it's being assigned an int
218+ expected ["E" ] = 1.0
219+ expected ["F" ] = 1.0
220+ assert_frame_equal (result , expected )
221+
222+ def test_setitem_list_some_missing_columns_list (self , float_frame ):
223+ # GH 26534
224+ result = float_frame .copy ()
225+ result [["A" , "E" ]] = [1 , 2 ]
226+ expected = float_frame .copy ()
227+ # force the dtypes to be float as currently multcolumn assignment does not
228+ # change column dtype from float to int even when it's being assigned an int
229+ expected ["A" ] = 1.0
230+ expected ["E" ] = 2.0
231+ assert_frame_equal (result , expected )
232+
233+ def test_setitem_list_some_missing_columns_dataframe (self , float_frame ):
234+ # GH 26534
235+ result = float_frame .copy ()
236+ result [["A" , "E" ]] = float_frame [["B" , "C" ]]
237+ expected = float_frame .copy ()
238+ expected ["A" ] = float_frame ["B" ]
239+ expected ["E" ] = float_frame ["C" ]
240+ assert_frame_equal (result , expected )
241+
242+ def test_setitem_list_some_missing_columns_2dlist (self ):
243+ # GH 26534
244+ result = pd .DataFrame ([[1 , 2 ], [3 , 4 ], [5 , 6 ]], columns = ["A" , "B" ])
245+ result [["B" , "C" , "D" ]] = [[7 , 8 , 9 ], [10 , 11 , 12 ], [13 , 14 , 15 ]]
246+ expected = pd .DataFrame (
247+ [[1 , 7 , 8 , 9 ], [3 , 10 , 11 , 12 ], [5 , 13 , 14 , 15 ]],
248+ columns = ["A" , "B" , "C" , "D" ],
249+ )
250+ tm .assert_frame_equal (result , expected )
251+
211252 def test_setitem_mulit_index (self ):
212253 # GH7655, test that assigning to a sub-frame of a frame
213254 # with multi-index columns aligns both rows and columns
@@ -501,13 +542,6 @@ def test_setitem(self, float_frame):
501542 float_frame ["col6" ] = series
502543 tm .assert_series_equal (series , float_frame ["col6" ], check_names = False )
503544
504- msg = (
505- r"\"None of \[Float64Index\(\[.*dtype='float64'\)\] are in the"
506- r" \[columns\]\""
507- )
508- with pytest .raises (KeyError , match = msg ):
509- float_frame [np .random .randn (len (float_frame ) + 1 )] = 1
510-
511545 # set ndarray
512546 arr = np .random .randn (len (float_frame ))
513547 float_frame ["col9" ] = arr
@@ -1143,17 +1177,6 @@ def test_fancy_index_int_labels_exceptions(self, float_frame):
11431177 )
11441178 with pytest .raises (KeyError , match = msg ):
11451179 float_frame .ix [["foo" , "bar" , "baz" ]] = 1
1146- msg = (
1147- r"None of \[Index\(\['E'\], dtype='object'\)\] are in the"
1148- r" \[columns\]"
1149- )
1150- with pytest .raises (KeyError , match = msg ):
1151- float_frame .ix [:, ["E" ]] = 1
1152-
1153- # FIXME: don't leave commented-out
1154- # partial setting now allows this GH2578
1155- # pytest.raises(KeyError, float_frame.ix.__setitem__,
1156- # (slice(None, None), 'E'), 1)
11571180
11581181 def test_setitem_fancy_mixed_2d (self , float_string_frame ):
11591182
0 commit comments