Skip to content

fixes #26622: pd.MultiIndex.isin should fail when input args are too short #26623

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ MultiIndex
^^^^^^^^^^

- Bug in which incorrect exception raised by :class:`Timedelta` when testing the membership of :class:`MultiIndex` (:issue:`24570`)
- :meth:`MultiIndex.isin` now raises ``ValueError`` when items in values are not the same length as the number of levels in the MultiIndex (:issue:`26622`)
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this to 1.0.0?

-

I/O
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3175,6 +3175,14 @@ def _wrap_joined_index(self, joined, other):
@Appender(Index.isin.__doc__)
def isin(self, values, level=None):
if level is None:
# validate value length
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, we already do this sort of validation in _verify_integrity in the MI construction, so I think the MI is actually getting constructed correctly, but you have nan's in the levels.

I am not opposed to the check, but it seems that maybe the semantics of handling this are not exactly right, meaning what you passed in your tests is actually valid.

nlvl = len(self.levels)
for val in values:
if len(val) != nlvl:
raise ValueError('Length of each element in values must '
'match number of levels in MultiIndex. '
'len({}) != {}'.format(val, nlvl))

values = MultiIndex.from_tuples(values,
names=self.names).values
return algos.isin(self.values, values)
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexes/multi/test_contains.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

Expand Down Expand Up @@ -58,6 +60,25 @@ def test_isin():
assert result.dtype == np.bool_


@pytest.mark.parametrize('values, expected_msg',
[([('foo',), ('bar', 3), ('quux',)],
"len(('foo',)) != 2"),
([('foo', 2), ('bar', 3, 2), ('quux', 4)],
"len(('bar', 3, 2)) != 2")])
def test_isin_bad_values_raises(values, expected_msg):
idx = MultiIndex.from_arrays([
['qux', 'baz', 'foo', 'bar'],
np.arange(4)
])

msg = re.escape('Length of each element in values must '
'match number of levels in MultiIndex. '
+ expected_msg)

with pytest.raises(ValueError, match=msg):
idx.isin(values)


@pytest.mark.skipif(PYPY, reason="tuples cmp recursively on PyPy")
def test_isin_nan_not_pypy():
idx = MultiIndex.from_arrays([['foo', 'bar'], [1.0, np.nan]])
Expand Down