diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index aef82392590d3..0811adbeeeda0 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -240,7 +240,7 @@ def _iterate_slices(self) -> Iterable[Series]: input="series", examples=_apply_docs["series_examples"] ) ) - def apply(self, func, *args, **kwargs): + def apply(self, func, *args, **kwargs) -> Series: return super().apply(func, *args, **kwargs) @doc(_agg_template, examples=_agg_examples_doc, klass="Series") diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 65e8b238cd476..9886ab53fb9f3 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -924,7 +924,9 @@ def _make_wrapper(self, name: str) -> Callable: # as are not passed directly but in the grouper f = getattr(self._obj_with_exclusions, name) if not isinstance(f, types.MethodType): - return self.apply(lambda self: getattr(self, name)) + # error: Incompatible return value type + # (got "NDFrameT", expected "Callable[..., Any]") [return-value] + return cast(Callable, self.apply(lambda self: getattr(self, name))) f = getattr(type(self._obj_with_exclusions), name) sig = inspect.signature(f) @@ -1350,7 +1352,7 @@ def _aggregate_with_numba(self, data, func, *args, engine_kwargs=None, **kwargs) input="dataframe", examples=_apply_docs["dataframe_examples"] ) ) - def apply(self, func, *args, **kwargs): + def apply(self, func, *args, **kwargs) -> NDFrameT: func = com.is_builtin_func(func) @@ -1410,7 +1412,7 @@ def _python_apply_general( f: Callable, data: DataFrame | Series, not_indexed_same: bool | None = None, - ) -> DataFrame | Series: + ) -> NDFrameT: """ Apply function f in python space @@ -1804,7 +1806,7 @@ def all(self, skipna: bool = True): @final @Substitution(name="groupby") @Appender(_common_see_also) - def count(self) -> Series | DataFrame: + def count(self) -> NDFrameT: """ Compute count of group, excluding missing values. @@ -3459,7 +3461,7 @@ def shift(self, periods=1, freq=None, axis=0, fill_value=None): @final @Substitution(name="groupby") @Appender(_common_see_also) - def diff(self, periods: int = 1, axis: int = 0) -> Series | DataFrame: + def diff(self, periods: int = 1, axis: int = 0) -> NDFrameT: """ First discrete difference of element.