Skip to content

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

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 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 @@ -3095,7 +3095,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):
try:
value = Series(value)
except (ValueError, NotImplementedError, TypeError):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,14 @@ def test_setitem_empty_frame_with_boolean(self, dtype, kwargs):
df[df > df2] = 47
assert_frame_equal(df, df2)

def test_setitem_with_empty_listlike(self):
# GH #17101
index = pd.Index([], name="idx")
result = pd.DataFrame(columns=["A"], index=index)
result["A"] = []
expected = pd.DataFrame(columns=["A"], index=index)
tm.assert_index_equal(result.index, expected.index)

def test_setitem_scalars_no_index(self):
# GH16823 / 17894
df = DataFrame()
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 @@ -442,10 +442,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 @@ -469,22 +469,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