diff --git a/pandas/_typing.py b/pandas/_typing.py index 99c46d21844f3..c79942c48509e 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -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]] diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 801d4665f9a1b..c0c6c9084b560 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -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 ( @@ -27,7 +27,7 @@ def frame_apply( obj: "DataFrame", - func, + func: AggFuncType, axis: Axis = 0, raw: bool = False, result_type: Optional[str] = None, diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 86a40f0845fd9..f2e833dfe7790 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -62,6 +62,7 @@ IndexLabel, Label, Level, + PythonFuncType, Renamer, StorageOptions, Suffixes, @@ -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. @@ -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.