Skip to content

Commit 8da0378

Browse files
committed
BUG: make sure partial setting with a Series like works with a completly empty frame (GH5632)
1 parent 6b2c5fd commit 8da0378

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

doc/source/release.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ API Changes
246246
(:issue:`4390`)
247247
- allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when
248248
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`)
250250
- Default export for ``to_clipboard`` is now csv with a sep of `\t` for
251251
compat (:issue:`3368`)
252252
- ``at`` now will enlarge the object inplace (and return the same)

pandas/core/frame.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1895,9 +1895,16 @@ def _ensure_valid_index(self, value):
18951895
passed value
18961896
"""
18971897
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+
18981905
if not isinstance(value, Series):
18991906
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')
19011908
self._data.set_axis(1, value.index.copy(), check_axis=False)
19021909

19031910
def _set_item(self, key, value):

pandas/tests/test_indexing.py

+36
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,42 @@ def f():
16351635
df.loc[:,1] = 1
16361636
self.assertRaises(ValueError, f)
16371637

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()
16381674
df2 = DataFrame()
16391675
df2[1] = Series([1],index=['foo'])
16401676
df.loc[:,1] = Series([1],index=['foo'])

0 commit comments

Comments
 (0)