Skip to content

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

Closed
@richierocks

Description

@richierocks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions