Skip to content

Commit

Permalink
Backport PR #51605 on branch 2.0.x (REGR: MultiIndex.isin with empty …
Browse files Browse the repository at this point in the history
…iterable raising) (#51617)

Backport PR #51605: REGR: MultiIndex.isin with empty iterable raising

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Feb 24, 2023
1 parent 03272ab commit 0c5a570
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3748,6 +3748,8 @@ def delete(self, loc) -> MultiIndex:
@doc(Index.isin)
def isin(self, values, level=None) -> npt.NDArray[np.bool_]:
if level is None:
if len(values) == 0:
return np.zeros((len(self),), dtype=np.bool_)
if not isinstance(values, MultiIndex):
values = MultiIndex.from_tuples(values)
return values.unique().get_indexer_for(self) != -1
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/multi/test_isin.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ def test_isin_multi_index_with_missing_value(labels, expected, level):
midx = MultiIndex.from_arrays([[np.nan, "a", "b"], ["c", "d", np.nan]])
result = midx.isin(labels, level=level)
tm.assert_numpy_array_equal(result, expected)


def test_isin_empty():
# GH#51599
midx = MultiIndex.from_arrays([[1, 2], [3, 4]])
result = midx.isin([])
expected = np.array([False, False])
tm.assert_numpy_array_equal(result, expected)

0 comments on commit 0c5a570

Please sign in to comment.