Skip to content

BUG: MultiIndex.isin() raises TypeError when given a generator in v2, works in v1.5 #52568

Closed
@tcuisset

Description

@tcuisset

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# Works
pd.Index([0]).isin((x for x in [0]))

# Used to work in v1.5, raises TypeError in v2
pd.MultiIndex.from_tuples([(1, 2)]).isin((x for x in [(1, 2)]))

Issue Description

Calling MultiIndex.isin(values) with values being a generator expression raises TypeError: object of type 'generator' has no len() in Pandas v2, whilst it worked in Pandas v1.5.
Index.isin still works with a generator expression.

The regression seems to come from #51605 .

Expected Behavior

Generator expressions to work with isin (or at least to behave consistently between Index.isin and MultiIndex.isin)

Installed Versions

Tested on latest pandas :

INSTALLED VERSIONS ------------------ commit : e9e034b python : 3.10.6.final.0 python-bits : 64 OS : Linux OS-release : 5.15.0-69-generic Version : #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.1.0.dev0+463.ge9e034badb
numpy : 1.25.0.dev0+1075.g1307defe4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

and on v1.5 :

INSTALLED VERSIONS

commit : 2e218d1
python : 3.10.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-69-generic
Version : #76-Ubuntu SMP Fri Mar 17 17:19:29 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 59.6.0
pip : 22.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions