Skip to content

BUG: Replacing categorical values with NA raises "boolean value of NA is ambiguous" error #46884

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

Closed
2 of 3 tasks
richierocks opened this issue Apr 27, 2022 · 2 comments · Fixed by #50857
Closed
2 of 3 tasks
Labels
Bug Categorical Categorical Data Type replace replace method

Comments

@richierocks
Copy link

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
phonetic = pd.DataFrame({
    "x": ["alpha", "bravo", "unknown", "delta"]
})
phonetic["x"] = pd.Categorical(phonetic["x"], ["alpha", "bravo", "charlie", "delta", "unknown"])
phonetic.replace("unknown", pd.NA)

Issue Description

When replacing values in a categorical series with NA, I see the error "boolean value of NA is ambiguous".

Expected Behavior

If we replace with NumPy's NaN value instead of pandas' NA, it works as expected.

import numpy as np
import pandas as pd
phonetic = pd.DataFrame({
    "x": ["alpha", "bravo", "unknown", "delta"]
})
phonetic["x"] = pd.Categorical(phonetic["x"], ["alpha", "bravo", "charlie", "delta", "unknown"])
phonetic.replace("unknown", np.nan)

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.176-91.338.amzn2.x86_64
Version : #1 SMP Fri Feb 4 16:59:59 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.2
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
pip : 21.3.1
setuptools : 57.0.0
Cython : 0.29.28
pytest : 6.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : 1.0.2
psycopg2 : 2.9.1
jinja2 : 3.0.1
IPython : 7.26.0
pandas_datareader: 0.10.0
bs4 : 4.9.3
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : 2.0.1
matplotlib : 3.3.4
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 5.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
snappy : None
sqlalchemy : 1.3.23
tables : 3.7.0
tabulate : 0.8.9
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None

@richierocks richierocks added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 27, 2022
@richierocks
Copy link
Author

Possibly loosely related to #32075

@richierocks
Copy link
Author

I see the same issue using the dict form of replace. phonetic.replace({"x": "unknown"}, pd.NA).

@richierocks richierocks changed the title BUG: Replacing cateogorical values with NA raises "boolean value of NA is ambiguous" error BUG: Replacing categorical values with NA raises "boolean value of NA is ambiguous" error Apr 27, 2022
@simonjayhawkins simonjayhawkins added Categorical Categorical Data Type replace replace method and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type replace replace method
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants