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: Inconsistent treatment of NaNs when .apply() function is used on categorical columns #59938

Closed
3 tasks done
apamplifi opened this issue Oct 2, 2024 · 2 comments · Fixed by #59966
Closed
3 tasks done
Assignees
Labels
Apply Apply, Aggregate, Transform, Map Bug Categorical Categorical Data Type good first issue
Milestone

Comments

@apamplifi
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

df = pd.DataFrame(
    {
        "a": [4, np.nan, 6],
        "b": ["one", "two", np.nan]
    }
)
df["b"] = df["b"].astype("category")

df["a'"] = df["a"].apply(lambda x: pd.notnull(x))  # rows with NaNs are treated
df["b'"] = df["b"].apply(lambda x: pd.notnull(x))  # rows with NaNs are skipped
display(df)

Issue Description

There is an inconsistency in how DataFrame.apply() function works on columns with categorical data, vs columns with any other type of the data. Generally speaking, .apply() function is called for every row of data (assuming axis=0 here), and then the user-defined function would be called on that row. This happens for all values of data, including NaNs, so if a special behaviour for NaNs is needed, it can be integrated easily. However, if the data column is of type category, the rows with NaNs appear to be automatically skipped, so rows with NaNs cannot be processed by the user.

In my opinion, this is a fundamental inconsistency, which I would call a bug. I would understand that in some situations the skipping of the NaN rows might be a preferred behaviour, but then it should probably be controllable via keyword arguments and certainly not datatype-dependent.

Expected Behavior

df["b'"] = df["b"].astype("string").apply(lambda x: pd.notnull(x)) # would work correctly...

...but I don't quite understand why such a fundamental behavior needs to be dependent on the type of the data.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.11.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : AMD64 Family 25 Model 68 Stepping 1, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.0.2
pip : 23.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.26.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : 2024.5.0
fsspec : 2024.6.1
gcsfs : None
matplotlib : 3.9.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@apamplifi apamplifi added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 2, 2024
@rhshadrach
Copy link
Member

Thanks for the report - this was worked on in #51645 where the behavior was deprecated for map. Currently in main, the corresponding change has not be made to apply.

pandas/pandas/core/apply.py

Lines 1474 to 1475 in fd823d2

action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
mapped = obj._map_values(mapper=curried, na_action=action)

I think we can consider this aspect of apply a bugfix and do not need to deprecate. PRs to fix are welcome!

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map good first issue Categorical Categorical Data Type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 2, 2024
@rhshadrach rhshadrach added this to the 3.0 milestone Oct 2, 2024
@Maru5er Maru5er removed their assignment Oct 3, 2024
@saldanhad
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Categorical Categorical Data Type good first issue
Projects
None yet
4 participants