Skip to content

TYP: Update type results for Groupby.count() and Groupby.size() #45904

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 15 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 7 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down