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

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ Other Removals
- Removed the ``method`` keyword in ``ExtensionArray.fillna``, implement ``ExtensionArray._pad_or_backfill`` instead (:issue:`53621`)
- Removed the attribute ``dtypes`` from :class:`.DataFrameGroupBy` (:issue:`51997`)
- Enforced deprecation of ``argmin``, ``argmax``, ``idxmin``, and ``idxmax`` returning a result when ``skipna=False`` and an NA value is encountered or all values are NA values; these operations will now raise in such cases (:issue:`33941`, :issue:`51276`)
- Removed support for ``action="ignore"`` for :class:`SeriesApply` in :meth:`SeriesApply.apply_standard`, categorical NA value now returns ``False`` (:issue:`59938`)
saldanhad marked this conversation as resolved.
Show resolved Hide resolved

.. ---------------------------------------------------------------------------
.. _whatsnew_300.performance:
Expand Down
16 changes: 5 additions & 11 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@
is_numeric_dtype,
is_sequence,
)
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
ExtensionDtype,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
from pandas.core.dtypes.generic import (
ABCDataFrame,
ABCNDFrame,
Expand Down Expand Up @@ -1466,13 +1463,10 @@ def curried(x):
else:
curried = func

# row-wise access
# apply doesn't have a `na_action` keyword and for backward compat reasons
# we need to give `na_action="ignore"` for categorical data.
# TODO: remove the `na_action="ignore"` when that default has been changed in
# Categorical (GH51645).
action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
mapped = obj._map_values(mapper=curried, na_action=action)
# remove the `na_action="ignore"` as default has been changed in
# Categorical (GH 51645).
# Reference for below fix (GH 59966)
saldanhad marked this conversation as resolved.
Show resolved Hide resolved
mapped = obj._map_values(mapper=curried)

if len(mapped) and isinstance(mapped[0], ABCSeries):
# GH#43986 Need to do list(mapped) in order to get treated as nested
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,8 @@ def test_apply_category_equalness(val):

result = df.a.apply(lambda x: x == val)
expected = Series(
[np.nan if pd.isnull(x) else x == val for x in df_values], name="a"
)
[False if pd.isnull(x) else x == val for x in df_values], name="a"
) # False since behavior of NaN for categorical dtype has been changed (GH 59966)
saldanhad marked this conversation as resolved.
Show resolved Hide resolved
tm.assert_series_equal(result, expected)


Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ def test_apply_categorical_with_nan_values(series, by_row):
with pytest.raises(AttributeError, match=msg):
s.apply(lambda x: x.split("-")[0], by_row=by_row)
return

result = s.apply(lambda x: x.split("-")[0], by_row=by_row)
# NaN for cat dtype fixed in (GH 59966)
result = s.apply(lambda x: x.split("-")[0] if pd.notna(x) else False, by_row=by_row)
result = result.astype(object)
expected = Series(["1", "1", np.nan], dtype="category")
expected = Series(["1", "1", False], dtype="category")
expected = expected.astype(object)
tm.assert_series_equal(result, expected)

Expand Down