Skip to content

ENH: make DataFrame.applymap uses the .map method of ExtensionArrays #52219

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Other enhancements
- :class:`api.extensions.ExtensionArray` now has a :meth:`~api.extensions.ExtensionArray.map` method (:issue:`51809`)
- Improve error message when having incompatible columns using :meth:`DataFrame.merge` (:issue:`51861`)
- Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue:`52084`)
- :meth:`DataFrame.applymap` now uses the :meth:`~api.extensions.ExtensionArray.map` method of underlying :class:`api.extensions.ExtensionArray` instances (:issue:`52219`)
- :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`).

.. ---------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9955,14 +9955,14 @@ def applymap(
raise ValueError(
f"na_action must be 'ignore' or None. Got {repr(na_action)}"
)
ignore_na = na_action == "ignore"

if self.empty:
return self.copy()

func = functools.partial(func, **kwargs)

# if we have a dtype == 'M8[ns]', provide boxed values
def infer(x):
if x.empty:
return lib.map_infer(x, func, ignore_na=ignore_na)
return lib.map_infer(x.astype(object)._values, func, ignore_na=ignore_na)
return x._map_values(func, na_action=na_action)

return self.apply(infer).__finalize__(self, "applymap")

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,29 @@ def test_applymap_float_object_conversion(val):
assert result == object


@pytest.mark.parametrize("na_action", [None, "ignore"])
def test_applymap_keeps_dtype(na_action):
# GH52219
arr = Series(["a", np.nan, "b"])
sparse_arr = arr.astype(pd.SparseDtype(object))
df = DataFrame(data={"a": arr, "b": sparse_arr})

def func(x):
return str.upper(x) if not pd.isna(x) else x

result = df.applymap(func, na_action=na_action)

expected_sparse = pd.array(["A", np.nan, "B"], dtype=pd.SparseDtype(object))
expected_arr = expected_sparse.astype(object)
expected = DataFrame({"a": expected_arr, "b": expected_sparse})

tm.assert_frame_equal(result, expected)

result_empty = df.iloc[:0, :].applymap(func, na_action=na_action)
expected_empty = expected.iloc[:0, :]
tm.assert_frame_equal(result_empty, expected_empty)


def test_applymap_str():
# GH 2786
df = DataFrame(np.random.random((3, 4)))
Expand Down