Skip to content

Backport PR #52572 on branch 2.0.x (REGR: MultiIndex.isin raising TypeError for generator) #52602

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
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/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Fixed regressions
- Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`)
- Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`)
- Fixed regression in :meth:`DataFrame.sort_values` not resetting index when :class:`DataFrame` is already sorted and ``ignore_index=True`` (:issue:`52553`)
- Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.bug_fixes:
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Any,
Callable,
Collection,
Generator,
Hashable,
Iterable,
List,
Expand Down Expand Up @@ -3749,6 +3750,9 @@ def delete(self, loc) -> MultiIndex:

@doc(Index.isin)
def isin(self, values, level=None) -> npt.NDArray[np.bool_]:
if isinstance(values, Generator):
values = list(values)

if level is None:
if len(values) == 0:
return np.zeros((len(self),), dtype=np.bool_)
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 @@ -93,3 +93,11 @@ def test_isin_empty():
result = midx.isin([])
expected = np.array([False, False])
tm.assert_numpy_array_equal(result, expected)


def test_isin_generator():
# GH#52568
midx = MultiIndex.from_tuples([(1, 2)])
result = midx.isin(x for x in [(1, 2)])
expected = np.array([True])
tm.assert_numpy_array_equal(result, expected)