Skip to content

BUG: pd.Series.isin returns inconsistent results for a passed iterable depending on the dtype #50234

@aberres

Description

@aberres

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

df = pd.DataFrame({"int": [1, 2, 3], "float": [1.0, 2.0, 3.0]})
filter_ = [1, 2]

print('## Good')
print(df["int"].isin([1, 2]))
print(df["float"].isin([1, 2]))
print(df["int"].isin(i for i in [1.0, 2.0]))
print('## Bad')
print(df["float"].isin(i for i in [1.0, 2.0]))

Issue Description

Passing an iterator to pd.Series.isin does not seem to work reliably. Depending on the dtype (I checked int and float`), the behavior differs.

The int dtype filters as I would expect (even when passing floats) while the float dtype always returns False.

I was running into this issue in production where all but one column were ints.

Expected Behavior

The output is always

0     True
1     True
2    False

Or at least consistent independent of the type.

Instead the last case returns

0    False
1    False
2    False

Installed Versions

Details

INSTALLED VERSIONS

commit : 8dab54d
python : 3.10.5.final.0
python-bits : 64
OS : Darwin
OS-release : 22.1.0
Version : Darwin Kernel Version 22.1.0: Sun Oct 9 20:14:54 PDT 2022; root:xnu-8792.41.9~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.5.2
numpy : 1.23.5
pytz : 2022.6
dateutil : 2.8.2
setuptools : 65.6.3
pip : 22.3.1
Cython : None
pytest : 7.2.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.1
pymysql : None
psycopg2 : 2.9.5
jinja2 : 3.1.2
IPython : 8.6.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : 2022.11.0
gcsfs : 2022.11.0
matplotlib : 3.6.2
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 10.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : 1.4.44
tables : None
tabulate : 0.9.0
xarray : 0.21.1
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : None

Metadata

Metadata

Labels

AlgosNon-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diffNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions