@@ -804,3 +804,35 @@ def test_loc_uint64(self):
804
804
result = s .loc [[np .iinfo ('uint64' ).max - 1 ,
805
805
np .iinfo ('uint64' ).max ]]
806
806
tm .assert_series_equal (result , s )
807
+
808
+ def test_loc_setitem_empty_append (self ):
809
+ # GH6173, various appends to an empty dataframe
810
+
811
+ data = [1 , 2 , 3 ]
812
+ expected = DataFrame ({'x' : data , 'y' : [None ] * len (data )})
813
+
814
+ # appends to fit length of data
815
+ df = DataFrame (columns = ['x' , 'y' ])
816
+ df .loc [:, 'x' ] = data
817
+ tm .assert_frame_equal (df , expected )
818
+
819
+ # only appends one value
820
+ expected = DataFrame ({'x' : [1.0 ], 'y' : [np .nan ]})
821
+ df = DataFrame (columns = ['x' , 'y' ],
822
+ dtype = np .float )
823
+ df .loc [0 , 'x' ] = expected .loc [0 , 'x' ]
824
+ tm .assert_frame_equal (df , expected )
825
+
826
+ def test_loc_setitem_empty_append_raises (self ):
827
+ # GH6173, various appends to an empty dataframe
828
+
829
+ data = [1 , 2 ]
830
+ df = DataFrame (columns = ['x' , 'y' ])
831
+ msg = (r"None of \[Int64Index\(\[0, 1\], dtype='int64'\)\] "
832
+ r"are in the \[index\]" )
833
+ with pytest .raises (KeyError , match = msg ):
834
+ df .loc [[0 , 1 ], 'x' ] = data
835
+
836
+ msg = "cannot copy sequence with size 2 to array axis with dimension 0"
837
+ with pytest .raises (ValueError , match = msg ):
838
+ df .loc [0 :2 , 'x' ] = data
0 commit comments