From d413a69c8ab97abab2273fe2110043d4147074c3 Mon Sep 17 00:00:00 2001 From: "Paulo S. Costa" Date: Sun, 7 Jan 2024 12:41:45 -0800 Subject: [PATCH] Type groupby pipe with function params --- pandas/core/groupby/groupby.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c9beaee55d608..f1ca05a312d2a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -29,6 +29,7 @@ class providing the base-class of operations. Union, cast, final, + overload, ) import warnings @@ -55,7 +56,6 @@ class providing the base-class of operations. PositionalIndexer, RandomState, Scalar, - T, npt, ) from pandas.compat.numpy import function as nv @@ -147,7 +147,13 @@ class providing the base-class of operations. ) if TYPE_CHECKING: - from typing import Any + from pandas._typing import ( + Any, + Concatenate, + P, + Self, + T, + ) from pandas.core.resample import Resampler from pandas.core.window import ( @@ -988,6 +994,24 @@ def _selected_obj(self): def _dir_additions(self) -> set[str]: return self.obj._dir_additions() + @overload + def pipe( + self, + func: Callable[Concatenate[Self, P], T], + *args: P.args, + **kwargs: P.kwargs, + ) -> T: + ... + + @overload + def pipe( + self, + func: tuple[Callable[..., T], str], + *args: Any, + **kwargs: Any, + ) -> T: + ... + @Substitution( klass="GroupBy", examples=dedent( @@ -1013,9 +1037,9 @@ def _dir_additions(self) -> set[str]: @Appender(_pipe_template) def pipe( self, - func: Callable[..., T] | tuple[Callable[..., T], str], - *args, - **kwargs, + func: Callable[Concatenate[Self, P], T] | tuple[Callable[..., T], str], + *args: Any, + **kwargs: Any, ) -> T: return com.pipe(self, func, *args, **kwargs)