Skip to content

BUG: make sure partial setting with a Series like works with a completly empty frame (GH5632) #5633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ API Changes
(:issue:`4390`)
- allow ``ix/loc`` for Series/DataFrame/Panel to set on any axis even when
the single-key is not currently contained in the index for that axis
(:issue:`2578`, :issue:`5226`)
(:issue:`2578`, :issue:`5226`, :issue:`5632`)
- Default export for ``to_clipboard`` is now csv with a sep of `\t` for
compat (:issue:`3368`)
- ``at`` now will enlarge the object inplace (and return the same)
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1895,9 +1895,16 @@ def _ensure_valid_index(self, value):
passed value
"""
if not len(self.index):

# GH5632, make sure that we are a Series convertible
try:
value = Series(value)
except:
pass

if not isinstance(value, Series):
raise ValueError('Cannot set a frame with no defined index '
'and a non-series')
'and a value that cannot be converted to a Series')
self._data.set_axis(1, value.index.copy(), check_axis=False)

def _set_item(self, key, value):
Expand Down
36 changes: 36 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,42 @@ def f():
df.loc[:,1] = 1
self.assertRaises(ValueError, f)

# these work as they don't really change
# anything but the index
# GH5632
expected = DataFrame(columns=['foo'])
def f():
df = DataFrame()
df['foo'] = Series([])
return df
assert_frame_equal(f(), expected)
def f():
df = DataFrame()
df['foo'] = Series(df.index)
return df
assert_frame_equal(f(), expected)
def f():
df = DataFrame()
df['foo'] = Series(range(len(df)))
return df
assert_frame_equal(f(), expected)
def f():
df = DataFrame()
df['foo'] = []
return df
assert_frame_equal(f(), expected)
def f():
df = DataFrame()
df['foo'] = df.index
return df
assert_frame_equal(f(), expected)
def f():
df = DataFrame()
df['foo'] = range(len(df))
return df
assert_frame_equal(f(), expected)

df = DataFrame()
df2 = DataFrame()
df2[1] = Series([1],index=['foo'])
df.loc[:,1] = Series([1],index=['foo'])
Expand Down