Skip to content

ENH add na_action to DataFrame.applymap #35704

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 5 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ For example:

Other enhancements
^^^^^^^^^^^^^^^^^^
- :meth:`applymap` now supports ``na_action`` (:issue:`23803`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't render, use DataFrame.applymap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was surprised by similar usage elsewhere in this file, which I copied:
https://github.com/pandas-dev/pandas/blob/6325a33/doc/source/whatsnew/v1.2.0.rst#L35

- :class:`Index` with object dtype supports division and multiplication (:issue:`34160`)
-
-
Expand Down
8 changes: 7 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2360,14 +2360,17 @@ def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask, bint convert=Tr

@cython.boundscheck(False)
@cython.wraparound(False)
def map_infer(ndarray arr, object f, bint convert=True):
def map_infer(ndarray arr, object f, bint convert=True, bint ignore_na=False):
"""
Substitute for np.vectorize with pandas-friendly dtype inference.

Parameters
----------
arr : ndarray
f : function
convert : bint
ignore_na : bint
If True, NA values will not have f applied

Returns
-------
Expand All @@ -2381,6 +2384,9 @@ def map_infer(ndarray arr, object f, bint convert=True):
n = len(arr)
result = np.empty(n, dtype=object)
for i in range(n):
if ignore_na and checknull(arr[i]):
result[i] = arr[i]
continue
val = f(arr[i])

if cnp.PyArray_IsZeroDim(val):
Expand Down
25 changes: 22 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7584,7 +7584,7 @@ def apply(self, func, axis=0, raw=False, result_type=None, args=(), **kwds):
)
return op.get_result()

def applymap(self, func) -> "DataFrame":
def applymap(self, func, na_action=None) -> "DataFrame":
"""
Apply a function to a Dataframe elementwise.

Expand All @@ -7595,6 +7595,10 @@ def applymap(self, func) -> "DataFrame":
----------
func : callable
Python function, returns a single value from a single value.
na_action : {None, 'ignore'}, default None
If ‘ignore’, propagate NaN values, without passing them to func.

.. versionadded:: 1.1

Returns
-------
Expand All @@ -7618,6 +7622,15 @@ def applymap(self, func) -> "DataFrame":
0 3 4
1 5 5

Like Series.map, NA values can be ignored:

>>> df_copy = df.copy()
>>> df_copy.iloc[0, 0] = pd.NA
>>> df_copy.applymap(lambda x: len(str(x)), na_action='ignore')
0 1
0 <NA> 4
1 5 5

Note that a vectorized version of `func` often exists, which will
be much faster. You could square each number elementwise.

Expand All @@ -7633,11 +7646,17 @@ def applymap(self, func) -> "DataFrame":
0 1.000000 4.494400
1 11.262736 20.857489
"""
if na_action not in {"ignore", None}:
raise ValueError(
f"na_action must be 'ignore' or None. Got {repr(na_action)}"
)
ignore_na = na_action == "ignore"

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

return self.apply(infer)

Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,20 @@ def test_applymap(self, float_frame):
tm.assert_frame_equal(applied, float_frame * 2)
float_frame.applymap(type)

# GH 23803: na_ignore
strlen_frame = float_frame.applymap(lambda x: len(str(x)))
float_frame_with_na = float_frame.copy()
mask = np.random.randint(0, 2, size=float_frame.shape, dtype=bool)
float_frame_with_na[mask] = pd.NA
strlen_frame_na_ignore = float_frame_with_na.applymap(
lambda x: len(str(x)), na_action="ignore"
)
strlen_frame_with_na = strlen_frame.copy()
strlen_frame_with_na[mask] = pd.NA
print(strlen_frame_na_ignore)
print(strlen_frame_with_na)
tm.assert_frame_equal(strlen_frame_na_ignore, strlen_frame_with_na)

# GH 465: function returning tuples
result = float_frame.applymap(lambda x: (x, x))
assert isinstance(result["A"][0], tuple)
Expand Down