File tree 3 files changed +45
-2
lines changed
3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -246,7 +246,7 @@ API Changes
246
246
(:issue: `4390 `)
247
247
- allow ``ix/loc `` for Series/DataFrame/Panel to set on any axis even when
248
248
the single-key is not currently contained in the index for that axis
249
- (:issue: `2578 `, :issue: `5226 `)
249
+ (:issue: `2578 `, :issue: `5226 `, :issue: ` 5632 ` )
250
250
- Default export for ``to_clipboard `` is now csv with a sep of `\t ` for
251
251
compat (:issue: `3368 `)
252
252
- ``at `` now will enlarge the object inplace (and return the same)
Original file line number Diff line number Diff line change @@ -1895,9 +1895,16 @@ def _ensure_valid_index(self, value):
1895
1895
passed value
1896
1896
"""
1897
1897
if not len (self .index ):
1898
+
1899
+ # GH5632, make sure that we are a Series convertible
1900
+ try :
1901
+ value = Series (value )
1902
+ except :
1903
+ pass
1904
+
1898
1905
if not isinstance (value , Series ):
1899
1906
raise ValueError ('Cannot set a frame with no defined index '
1900
- 'and a non-series ' )
1907
+ 'and a value that cannot be converted to a Series ' )
1901
1908
self ._data .set_axis (1 , value .index .copy (), check_axis = False )
1902
1909
1903
1910
def _set_item (self , key , value ):
Original file line number Diff line number Diff line change @@ -1635,6 +1635,42 @@ def f():
1635
1635
df .loc [:,1 ] = 1
1636
1636
self .assertRaises (ValueError , f )
1637
1637
1638
+ # these work as they don't really change
1639
+ # anything but the index
1640
+ # GH5632
1641
+ expected = DataFrame (columns = ['foo' ])
1642
+ def f ():
1643
+ df = DataFrame ()
1644
+ df ['foo' ] = Series ([])
1645
+ return df
1646
+ assert_frame_equal (f (), expected )
1647
+ def f ():
1648
+ df = DataFrame ()
1649
+ df ['foo' ] = Series (df .index )
1650
+ return df
1651
+ assert_frame_equal (f (), expected )
1652
+ def f ():
1653
+ df = DataFrame ()
1654
+ df ['foo' ] = Series (range (len (df )))
1655
+ return df
1656
+ assert_frame_equal (f (), expected )
1657
+ def f ():
1658
+ df = DataFrame ()
1659
+ df ['foo' ] = []
1660
+ return df
1661
+ assert_frame_equal (f (), expected )
1662
+ def f ():
1663
+ df = DataFrame ()
1664
+ df ['foo' ] = df .index
1665
+ return df
1666
+ assert_frame_equal (f (), expected )
1667
+ def f ():
1668
+ df = DataFrame ()
1669
+ df ['foo' ] = range (len (df ))
1670
+ return df
1671
+ assert_frame_equal (f (), expected )
1672
+
1673
+ df = DataFrame ()
1638
1674
df2 = DataFrame ()
1639
1675
df2 [1 ] = Series ([1 ],index = ['foo' ])
1640
1676
df .loc [:,1 ] = Series ([1 ],index = ['foo' ])
You can’t perform that action at this time.
0 commit comments