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 @@ -131,6 +131,8 @@
"BaseWindow",
"Resampler",
]
ApplyFuncType = Union[np.ufunc, AggFuncType]
PythonFuncType = Callable[[Any], Any]

# filenames and file-like-objects
Buffer = Union[IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]
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 ApplyFuncType, 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: ApplyFuncType,
axis: Axis = 0,
raw: bool = False,
result_type: Optional[str] = None,
Expand Down
14 changes: 12 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from pandas._libs.lib import no_default
from pandas._typing import (
AggFuncType,
ApplyFuncType,
ArrayLike,
Axes,
Axis,
Expand All @@ -61,6 +62,7 @@
IndexKeyFunc,
Label,
Level,
PythonFuncType,
Renamer,
StorageOptions,
ValueKeyFunc,
Expand Down Expand Up @@ -7656,7 +7658,13 @@ def transform(
return result

def apply(
self, func, axis: Axis = 0, raw: bool = False, result_type=None, args=(), **kwds
self,
func: ApplyFuncType,
Copy link
Member

@jorisvandenbossche jorisvandenbossche Dec 15, 2020

Choose a reason for hiding this comment

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

Just to make it explicit, this annotation will show up as Union[np.ufunc, Union[Callable, str, List[Union[Callable, str]], Dict[Union[Hashable, NoneType], Union[Callable, str, List[Union[Callable, str]]]]]]

(kind of unreadable, IMO, but so which is not even complete yet, since the ufunc should be present in each nested case Union[Callable, str]. But isn't a ufunc a callable, so that can maybe be removed?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Going off the aggregation case we don't special case ufunc there so we probably shouldn't do it here either.

In that case I think ApplyFuncType is the same as AggFuncType so we can use the same alias in both cases

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah i agree here, no need to be different, let's use AggFuncType

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

axis: Axis = 0,
raw: bool = False,
result_type=None,
args=(),
**kwds,
):
"""
Apply a function along an axis of the DataFrame.
Expand Down Expand Up @@ -7802,7 +7810,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