Skip to content

BUG: Series with NA values are replaced with NaN when mapping over categorial data with a default providing dictionary #62710

@Jake-Moss

Description

@Jake-Moss

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
from collections import defaultdict

df = pd.DataFrame(
    {
        "a": ["one", "two", pd.NA],
        "b": ["one", "two", pd.NA],
        "c": ["one", "two", pd.NA],
        "d": ["one", "two", pd.NA],
    }
)
df["b"] = df["b"].astype("category")
df["d"] = df["d"].astype("category")

df["a"] = df["a"].map(defaultdict(lambda: True), na_action=None)
df["b"] = df["b"].map(defaultdict(lambda: True), na_action=None)  # rows with NA are replaced by float("NaN")
df["c"] = df["c"].map(defaultdict(lambda: True), na_action="ignore")
df["d"] = df["d"].map(defaultdict(lambda: True), na_action="ignore")  # rows with NA are replaced by float("NaN")

assert not df["a"].isna().any()
assert not df["b"].isna().any()

# Check types with
df.applymap(type)
# Col b and d, row 2 is float rather than type(pd.NA)

Issue Description

Series with NA values (pd.NA, None, np.nan, float("NaN"), e.t.c) are skipped and replaced by float("NaN") when mapping over categorial data with a default providing dictionary. The na_action has no effect.

Expected Behavior

I would expect the NA values of categorical data to behave in the same manner as other dtypes, i.e. na_action is respected and the default value is used when appropriate.

Installed Versions

INSTALLED VERSIONS

commit : 9c8bc3e
python : 3.11.10
python-bits : 64
OS : Linux
OS-release : 6.6.87.2-microsoft-standard-WSL2
Version : #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.3
numpy : 2.3.4
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 24.0
Cython : None
sphinx : None
IPython : 9.6.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.14.2
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.9.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : None
matplotlib : 3.10.6
numba : 0.62.1
numexpr : 2.13.1
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 21.0.0
pyreadstat : None
pytest : 8.4.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.16.2
sqlalchemy : None
tables : 3.10.2
tabulate : None
xarray : 2025.6.1
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions