Skip to content
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

BUG: boolean series .isin([pd.NA])] inconsistent for series length #60678

Open
2 of 3 tasks
yoavrv opened this issue Jan 8, 2025 · 3 comments
Open
2 of 3 tasks

BUG: boolean series .isin([pd.NA])] inconsistent for series length #60678

yoavrv opened this issue Jan 8, 2025 · 3 comments
Labels
Bug isin isin method Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@yoavrv
Copy link

yoavrv commented Jan 8, 2025

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
ser = pd.Series(True, index=range(10_000_000), dtype='boolean')
ser.iloc[0] = pd.NA
ser.iloc[1] =False
result_short = ser.iloc[:10].isin([False, pd.NA])
assert result_short.sum() == 2
result_long = ser.isin([False, pd.NA])

Issue Description

The behavior of the .isin() function on nullable boolean boolean series is inconsistent for pd.NA:
For short series, this returns a series with True for every null values in the original series

>>> pd.Series([True, False, pd.NA], dtype='boolean').isin([pd.NA])
0    False
1    False
2     True
dtype: boolean

For long series, this raises a TypeError: boolean value of NA is ambiguous

>>> pd.Series(True, index=range(1_000_000), dtype='boolean').isin([pd.NA])
0         False
1         False
2         False
3         False
4         False
          ...  
999995    False
999996    False
999997    False
999998    False
999999    False
Length: 1000000, dtype: boolean
>>> pd.Series(True, index=range(1_000_001), dtype='boolean').isin([pd.NA])
Traceback (most recent call last):
  File "<python-input-34>", line 1, in <module>
    pd.Series(True, index=range(1_000_001), dtype='boolean').isin([pd.NA])
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/pandas/core/series.py", line 5559, in isin
    result = algorithms.isin(self._values, values)
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/pandas/core/algorithms.py", line 505, in isin
    return comps_array.isin(values)
           ~~~~~~~~~~~~~~~~^^^^^^^^
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/pandas/core/arrays/masked.py", line 971, in isin
    result = isin(self._data, values_arr)
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/pandas/core/algorithms.py", line 545, in isin
    return f(comps_array, values)
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/pandas/core/algorithms.py", line 534, in f
    return np.logical_or(np.isin(c, v).ravel(), np.isnan(c))
                         ~~~~~~~^^^^^^
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/numpy/lib/_arraysetops_impl.py", line 1132, in isin
    return _in1d(element, test_elements, assume_unique=assume_unique,
           ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 invert=invert, kind=kind).reshape(element.shape)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/homes/yoavr/.conda/envs/pandas_latest/lib/python3.13/site-packages/numpy/lib/_arraysetops_impl.py", line 982, in _in1d
    mask |= (ar1 == a)
             ^^^^^^^^
  File "missing.pyx", line 392, in pandas._libs.missing.NAType.__bool__
TypeError: boolean value of NA is ambiguous

Expected Behavior

.isin([pd.NA]) should either always work or it should always raise an error.

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.13.1
python-bits : 64
OS : Linux
OS-release : 5.15.0-88-generic
Version : #98-Ubuntu SMP Mon Oct 2 15:18:56 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : en_US.UTF-8
LOCALE : C.UTF-8

pandas : 2.2.3
numpy : 2.2.1
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@yoavrv yoavrv added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2025
@rhshadrach
Copy link
Member

Thanks for the report! Confirmed on main, further investigations and PRs to fix are welcome.

@rhshadrach rhshadrach added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate isin isin method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 8, 2025
@akj2018
Copy link

akj2018 commented Jan 8, 2025

Hi @rhshadrach. I would like to take this issue. Please assign it to me. Thanks!

@snitish
Copy link
Contributor

snitish commented Jan 9, 2025

@akj2018 you can comment take to assign it to yourself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug isin isin method Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

4 participants