Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 6 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,14 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None,
elif data is None or is_scalar(data):
cls._scalar_data_error(data)
else:
if tupleize_cols and is_list_like(data) and data:
if tupleize_cols and is_list_like(data):
# GH21470: convert iterable to list before determining if empty
if is_iterator(data):
data = list(data)
# we must be all tuples, otherwise don't construct
# 10697
if all(isinstance(e, tuple) for e in data):

if data and all(isinstance(e, tuple) for e in data):
# we must be all tuples, otherwise don't construct
# 10697
from .multi import MultiIndex
return MultiIndex.from_tuples(
data, names=name or kwargs.get('names'))
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,22 @@ def test_constructor_dtypes_timedelta(self, attr, klass):
result = klass(list(values), dtype=dtype)
tm.assert_index_equal(result, index)

def test_constructor_empty_gen(self):
def test_constructor_empty_list(self):
Copy link
Member

@mroeschke mroeschke Jun 14, 2018

Choose a reason for hiding this comment

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

Could you combine this test with the one below with pytest.mark.parametrize over skip_index_keys and the empty list and generator?

skip_index_keys = ["repeats", "periodIndex", "rangeIndex",
"tuples"]
for key, index in self.generate_index_types(skip_index_keys):
empty = index.__class__([])
assert isinstance(empty, index.__class__)
assert not len(empty)

def test_constructor_empty_gen(self):
skip_index_keys = ["repeats", "periodIndex", "rangeIndex",
"tuples"]
for key, index in self.generate_index_types(skip_index_keys):
empty = index.__class__(x for x in [])
assert isinstance(empty, index.__class__)
assert not len(empty)

@pytest.mark.parametrize("empty,klass", [
(PeriodIndex([], freq='B'), PeriodIndex),
(RangeIndex(step=1), pd.RangeIndex),
Expand Down