-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Comments
Thanks for the report! Confirmed on main, further investigations and PRs to fix are welcome. |
Hi @rhshadrach. I would like to take this issue. Please assign it to me. Thanks! |
@akj2018 you can comment take to assign it to yourself |
take |
Hi everyone, This is my first contribution to this repository and still learning. I’m open to constructive criticism and suggestions for improvement. If there are better ways to handle this scenario or additional areas where my fix could be improved, I’d greatly appreciate your insights! No inconsistency due to pd.NAAfter examining the error logs and debugging the reproducible example, I noticed
def isin(comps: ListLike, values: ListLike) -> npt.NDArray[np.bool_]:
"""
Compute the isin boolean array.
Parameters
----------
comps : list-like
values : list-like
Returns
-------
ndarray[bool]
Same length as `comps`.
"""
...
if (
len(comps_array) > _MINIMUM_COMP_ARR_LEN
and len(values) <= 26
and comps_array.dtype != object
):
# If the values include nan we need to check for nan explicitly
# since np.nan it not equal to np.nan
if isna(values).any():
def f(c, v):
return np.logical_or(np.isin(c, v).ravel(), np.isnan(c))
else:
f = lambda a, b: np.isin(a, b).ravel()
else:
common = np_find_common_type(values.dtype, comps_array.dtype)
values = values.astype(common, copy=False)
comps_array = comps_array.astype(common, copy=False)
f = htable.ismember
return f(comps_array, values) What this means
Conclusionlogic of Reason for TypeError: boolean value of NA is ambiguous
elif isinstance(values, ABCMultiIndex):
# Avoid raising in extract_array
values = np.array(values)
else:
values = extract_array(values, extract_numpy=True, extract_range=True)
comps_array = _ensure_arraylike(comps, func_name="isin")
What this meansIf values contains incompatible types (e.g., Proposed SolutionTo address this issue, I added a condition to exclude cases where values.dtype == object used if (
len(comps_array) > _MINIMUM_COMP_ARR_LEN
and len(values) <= 26
and comps_array.dtype != object
and values.dtype != object # New condition to handle object dtype in values
) TestingI have not yet run the tests but plan to do so once this approach is reviewed and agreed upon. If there are any edge cases I should focus on or additional scenarios you’d like me to test, please let me know! Looking forward to your feedback. |
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
Issue Description
The behavior of the
.isin()
function on nullable booleanboolean
series is inconsistent forpd.NA
:For short series, this returns a series with
True
for every null values in the original seriesFor long series, this raises a
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
The text was updated successfully, but these errors were encountered: