Skip to content

Commit 090fa81

Browse files
authored
REF: simplify Series.apply (#52033)
* REF: use IndexOpsMixin._map_values in Series.apply * fix docs
1 parent db12473 commit 090fa81

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

pandas/core/algorithms.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,10 @@ def union_with_duplicates(
16731673

16741674

16751675
def map_array(
1676-
arr: ArrayLike, mapper, na_action: Literal["ignore"] | None = None
1676+
arr: ArrayLike,
1677+
mapper,
1678+
na_action: Literal["ignore"] | None = None,
1679+
convert: bool = True,
16771680
) -> np.ndarray | ExtensionArray | Index:
16781681
"""
16791682
Map values using an input mapping or function.
@@ -1685,6 +1688,9 @@ def map_array(
16851688
na_action : {None, 'ignore'}, default None
16861689
If 'ignore', propagate NA values, without passing them to the
16871690
mapping correspondence.
1691+
convert : bool, default True
1692+
Try to find better dtype for elementwise function results. If
1693+
False, leave as dtype=object.
16881694
16891695
Returns
16901696
-------
@@ -1739,6 +1745,8 @@ def map_array(
17391745
# we must convert to python types
17401746
values = arr.astype(object, copy=False)
17411747
if na_action is None:
1742-
return lib.map_infer(values, mapper)
1748+
return lib.map_infer(values, mapper, convert=convert)
17431749
else:
1744-
return lib.map_infer_mask(values, mapper, isna(values).view(np.uint8))
1750+
return lib.map_infer_mask(
1751+
values, mapper, mask=isna(values).view(np.uint8), convert=convert
1752+
)

pandas/core/apply.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
from pandas._config import option_context
2626

27-
from pandas._libs import lib
2827
from pandas._typing import (
2928
AggFuncType,
3029
AggFuncTypeBase,
@@ -1063,20 +1062,12 @@ def apply_standard(self) -> DataFrame | Series:
10631062
f = cast(Callable, self.f)
10641063
obj = self.obj
10651064

1066-
with np.errstate(all="ignore"):
1067-
if isinstance(f, np.ufunc):
1065+
if isinstance(f, np.ufunc):
1066+
with np.errstate(all="ignore"):
10681067
return f(obj)
10691068

1070-
# row-wise access
1071-
if is_extension_array_dtype(obj.dtype):
1072-
mapped = obj._values.map(f)
1073-
else:
1074-
values = obj.astype(object)._values
1075-
mapped = lib.map_infer(
1076-
values,
1077-
f,
1078-
convert=self.convert_dtype,
1079-
)
1069+
# row-wise access
1070+
mapped = obj._map_values(mapper=f, convert=self.convert_dtype)
10801071

10811072
if len(mapped) and isinstance(mapped[0], ABCSeries):
10821073
# GH#43986 Need to do list(mapped) in order to get treated as nested

pandas/core/base.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ def _reduce(
865865
return func(skipna=skipna, **kwds)
866866

867867
@final
868-
def _map_values(self, mapper, na_action=None):
868+
def _map_values(self, mapper, na_action=None, convert: bool = True):
869869
"""
870870
An internal function that maps values using the input
871871
correspondence (which can be a dict, Series, or function).
@@ -877,6 +877,10 @@ def _map_values(self, mapper, na_action=None):
877877
na_action : {None, 'ignore'}
878878
If 'ignore', propagate NA values, without passing them to the
879879
mapping function
880+
convert : bool, default True
881+
Try to find better dtype for elementwise function results. If
882+
False, leave as dtype=object. Note that the dtype is always
883+
preserved for some extension array dtypes, such as Categorical.
880884
881885
Returns
882886
-------
@@ -894,7 +898,7 @@ def _map_values(self, mapper, na_action=None):
894898
# "Union[IndexOpsMixin, ExtensionArray, ndarray[Any, Any]]";
895899
# expected "Union[ExtensionArray, ndarray[Any, Any]]"
896900
return algorithms.map_array(
897-
arr, mapper, na_action=na_action # type: ignore[arg-type]
901+
arr, mapper, na_action=na_action, convert=convert # type: ignore[arg-type]
898902
)
899903

900904
@final

0 commit comments

Comments
 (0)