Skip to content
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

Fix bug where list like object not returning empty Index. #26471

Merged
merged 10 commits into from
Aug 22, 2019
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3402,7 +3402,7 @@ def _ensure_valid_index(self, value):
passed value.
"""
# GH5632, make sure that we are a Series convertible
if not len(self.index) and is_list_like(value):
if not len(self.index) and is_list_like(value) and len(value):
jreback marked this conversation as resolved.
Show resolved Hide resolved
try:
value = Series(value)
except (ValueError, NotImplementedError, TypeError):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,13 @@ def test_index_type_coercion(self):
idxr(s2)['0'] = 0
assert s2.index.is_object()

def test_empty_index_for_empty_dataframe(self):
index = pd.Index([], name='idx')
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
result = pd.DataFrame(columns=['A'], index=index)
result['A'] = []
expected = pd.DataFrame(columns=['A'], index=index)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
tm.assert_index_equal(result.index, expected.index)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved


class TestMisc(Base):

Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,10 @@ def test_partial_set_empty_frame(self):
# these work as they don't really change
# anything but the index
# GH5632
expected = DataFrame(columns=['foo'], index=Index([], dtype='int64'))
expected = DataFrame(columns=['foo'], index=Index([], dtype='object'))

def f():
df = DataFrame()
df = DataFrame(index=Index([], dtype='object'))
df['foo'] = Series([], dtype='object')
return df

Expand All @@ -478,22 +478,21 @@ def f():
expected['foo'] = expected['foo'].astype('float64')

def f():
df = DataFrame()
df = DataFrame(index=Index([], dtype='int64'))
df['foo'] = []
return df

tm.assert_frame_equal(f(), expected)

def f():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do these need nested functions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed it's strange, but we'll ignore that, since the modifications are following the existing structure.

df = DataFrame()
df = DataFrame(index=Index([], dtype='int64'))
df['foo'] = Series(np.arange(len(df)), dtype='float64')
return df

tm.assert_frame_equal(f(), expected)

def f():
df = DataFrame()
tm.assert_index_equal(df.index, Index([], dtype='object'))
df = DataFrame(index=Index([], dtype='int64'))
df['foo'] = range(len(df))
return df

Expand Down