8000 BUG: allow empty multiindex (fixes .isin regression, GH16777) by drudd · Pull Request #16782 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: allow empty multiindex (fixes .isin regression, GH16777) #16782

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 11 commits into from
Jul 7, 2017
Prev Previous commit
Next Next commit
Revert ability to construct MultiIndex from empty arrays, ensure all …
…tests pass despite issue #16844
  • Loading branch information
Douglas Rudd authored and jreback committed Jul 7, 2017
commit f94d2b4905d75c2a4e2328b7e90fd10d7a666fbf
14 changes: 9 additions & 5 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,9 @@ def from_arrays(cls, arrays, sortorder=None, names=None):
MultiIndex.from_product : Make a MultiIndex from cartesian product
of iterables
"""
if len(arrays) == 1:
if len(arrays) == 0:
raise ValueError('Must pass non-zero number of levels/labels')
elif len(arrays) == 1:
name = None if names is None else names[0]
return Index(arrays[0], name=name)

Expand Down Expand Up @@ -1131,10 +1133,12 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
MultiIndex.from_product : Make a MultiIndex from cartesian product
of iterables
"""
if len(tuples) == 0 and names is None:
raise TypeError('Cannot infer number of levels from empty list')

if isinstance(tuples, (np.ndarray, Index)):
if len(tuples) == 0:
if names is None:
msg = 'Cannot infer number of levels from empty list'
raise TypeError(msg)
arrays = [[]]*len(names)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is causing a lint issue

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

elif isinstance(tuples, (np.ndarray, Index)):
if isinstance(tuples, Index):
tuples = tuples._values

Expand Down
13 changes: 2 additions & 11 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,6 @@ def test_from_arrays_empty(self):
ValueError, "Must pass non-zero number of levels/labels"):
MultiIndex.from_arrays(arrays=[])

# 0 levels, names defined
result = MultiIndex.from_arrays(arrays=[],
names=['A', 'B'])
expected = MultiIndex(levels=[[], []],
labels=[[], []],
names=['A', 'B'])
tm.assert_index_equal(result, expected)

# 1 level
result = MultiIndex.from_arrays(arrays=[[]], names=['A'])
expected = Index([], name='A')
Expand Down Expand Up @@ -1726,9 +1718,8 @@ def test_from_tuples(self):
MultiIndex.from_tuples, [])

result = MultiIndex.from_tuples([], names=['a', 'b'])
Copy link
Contributor

Choose a reason for hiding this comment

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

similary I think [(), ()] should work here

Copy link
Member
@jorisvandenbossche jorisvandenbossche Jun 29, 2017

Choose a reason for hiding this comment

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

No, IMO this should not work here, as in this case you actually do pass 2 values, but empty values (not an empty container holding no values). So IMO this should raise.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with this. It should raise because you have a mismatch between the names and what you passe 8000 d. If you had NOT passed an empty list it would be ok.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you make the empty case a separately named test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

expected = MultiIndex(levels=[[], []],
labels=[[], []],
names=['a', 'b'])
expected = MultiIndex.from_arrays(arrays=[[], []],
names=['a', 'b'])
tm.assert_index_equal(result, expected)

idx = MultiIndex.from_tuples(((1, 2), (3, 4)), names=['a', 'b'])
Expand Down
0