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 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ For example:

Other enhancements
^^^^^^^^^^^^^^^^^^

- Added :meth:`~DataFrame.set_flags` for setting table-wide flags on a ``Series`` or ``DataFrame`` (:issue:`28394`)
- :meth:`DataFrame.applymap` now supports ``na_action`` (:issue:`23803`)
- :class:`Index` with object dtype supports division and multiplication (:issue:`34160`)
- :meth:`DataFrame.explode` and :meth:`Series.explode` now support exploding of sets (:issue:`35614`)
-
Expand Down
8 changes: 7 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2377,14 +2377,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 @@ -2398,6 +2401,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 @@ -7617,7 +7617,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: Optional[str] = None) -> DataFrame:
"""
Apply a function to a Dataframe elementwise.

Expand All @@ -7628,6 +7628,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.2

Returns
-------
Expand All @@ -7651,6 +7655,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 @@ -7666,11 +7679,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
16 changes: 16 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,22 @@ def test_applymap(self, float_frame):
result = frame.applymap(func)
tm.assert_frame_equal(result, frame)

def test_applymap_na_ignore(self, float_frame):
# GH 23803
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
tm.assert_frame_equal(strlen_frame_na_ignore, strlen_frame_with_na)

with pytest.raises(ValueError, match="na_action must be .*Got 'abc'"):
float_frame_with_na.applymap(lambda x: len(str(x)), na_action="abc")

def test_applymap_box_timestamps(self):
# GH 2689, GH 2627
ser = pd.Series(date_range("1/1/2000", periods=10))
Expand Down