Skip to content

TYP: func argument to DataFrame.apply, DataFrame.applymap, core.apply.frame_apply #38493

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 9 commits into from
Dec 18, 2020
2 changes: 2 additions & 0 deletions pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
"Resampler",
]

PythonFuncType = Callable[[Any], Any]

# filenames and file-like-objects
Buffer = Union[IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]
FileOrBuffer = Union[str, Buffer[T]]
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pandas._config import option_context

from pandas._typing import Axis, FrameOrSeriesUnion
from pandas._typing import AggFuncType, Axis, FrameOrSeriesUnion
from pandas.util._decorators import cache_readonly

from pandas.core.dtypes.common import (
Expand All @@ -27,7 +27,7 @@

def frame_apply(
obj: "DataFrame",
func,
func: AggFuncType,
axis: Axis = 0,
raw: bool = False,
result_type: Optional[str] = None,
Expand Down
13 changes: 11 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
IndexLabel,
Label,
Level,
PythonFuncType,
Renamer,
StorageOptions,
Suffixes,
Expand Down Expand Up @@ -7661,7 +7662,13 @@ def transform(
return result

def apply(
self, func, axis: Axis = 0, raw: bool = False, result_type=None, args=(), **kwds
self,
func: AggFuncType,
axis: Axis = 0,
raw: bool = False,
result_type=None,
args=(),
**kwds,
):
"""
Apply a function along an axis of the DataFrame.
Expand Down Expand Up @@ -7807,7 +7814,9 @@ def apply(
)
return op.get_result()

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

Expand Down