Skip to content

Commit

Permalink
Backport PR #52572 on branch 2.0.x (REGR: MultiIndex.isin raising Typ…
Browse files Browse the repository at this point in the history
…eError for generator) (#52602)

Backport PR #52572: REGR: MultiIndex.isin raising TypeError for generator

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl committed Apr 11, 2023
1 parent 092d5bf commit d3902a7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
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)

0 comments on commit d3902a7

Please sign in to comment.