Skip to content

Commit

Permalink
BUG: allow empty multiindex (fixes .isin regression, GH16777) (#16782)
Browse files Browse the repository at this point in the history
  • Loading branch information
Douglas Rudd authored and jreback committed Jul 7, 2017
1 parent f6edaef commit 500cd0f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Indexing
^^^^^^^^

- Bug in ``Float64Index`` causing an empty array instead of ``None`` to be returned from ``.get(np.nan)`` on a Series whose index did not contain any ``NaN`` s (:issue:`8569`)
- Bug in ``MultiIndex.isin`` causing an error when passing an empty iterable (:issue:`16777`)

I/O
^^^
Expand Down
14 changes: 8 additions & 6 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,10 +1129,11 @@ def from_tuples(cls, tuples, sortorder=None, names=None):
of iterables
"""
if len(tuples) == 0:
# I think this is right? Not quite sure...
raise TypeError('Cannot infer number of levels from empty list')

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

Expand Down Expand Up @@ -2621,8 +2622,9 @@ def _wrap_joined_index(self, joined, other):
@Appender(Index.isin.__doc__)
def isin(self, values, level=None):
if level is None:
return algos.isin(self.values,
MultiIndex.from_tuples(values).values)
values = MultiIndex.from_tuples(values,
names=self.names).values
return algos.isin(self.values, values)
else:
num = self._get_level_number(level)
levs = self.levels[num]
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,13 @@ def test_from_tuples(self):
idx = MultiIndex.from_tuples(((1, 2), (3, 4)), names=['a', 'b'])
assert len(idx) == 2

def test_from_tuples_empty(self):
# GH 16777
result = MultiIndex.from_tuples([], names=['a', 'b'])
expected = MultiIndex.from_arrays(arrays=[[], []],
names=['a', 'b'])
tm.assert_index_equal(result, expected)

def test_argsort(self):
result = self.index.argsort()
expected = self.index.values.argsort()
Expand Down

0 comments on commit 500cd0f

Please sign in to comment.