From 3ab2c3e7db0d043eff4ed57e2954993cd39623ac Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 10 Apr 2023 21:23:56 +0200 Subject: [PATCH 1/2] REGR: MultiIndex.isin raising TypeError for generator --- doc/source/whatsnew/v2.0.1.rst | 2 +- pandas/core/indexes/multi.py | 4 ++++ pandas/tests/indexes/multi/test_isin.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index caf237fb15163..a951ebb409ae0 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -15,7 +15,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:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 4de67f81dcfe0..85252de7ec8ad 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -7,6 +7,7 @@ Any, Callable, Collection, + Generator, Hashable, Iterable, List, @@ -3772,6 +3773,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_) diff --git a/pandas/tests/indexes/multi/test_isin.py b/pandas/tests/indexes/multi/test_isin.py index 2723e7e4db8eb..68fdf25359f1b 100644 --- a/pandas/tests/indexes/multi/test_isin.py +++ b/pandas/tests/indexes/multi/test_isin.py @@ -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) From be6ca95095363904b909975c5e7da5e10f84054c Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 11 Apr 2023 09:59:00 +0200 Subject: [PATCH 2/2] Update v2.0.1.rst --- doc/source/whatsnew/v2.0.1.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index a951ebb409ae0..34c665cbbd5bb 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -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:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`) + .. --------------------------------------------------------------------------- .. _whatsnew_201.bug_fixes: