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
17 changes: 16 additions & 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 Expand Up @@ -587,6 +587,12 @@ def nunique(self, dropna: bool = True) -> Series:
def describe(self, **kwargs):
return super().describe(**kwargs)

def count(self) -> Series:
return cast(Series, super().count())

def size(self) -> Series:
return cast(Series, super().size())

def value_counts(
self,
normalize: bool = False,
Expand Down Expand Up @@ -764,6 +770,9 @@ class DataFrameGroupBy(GroupBy[DataFrame]):

_apply_allowlist = base.dataframe_apply_allowlist

def apply(self, func, *args, **kwargs) -> DataFrame:
return super().apply(func, *args, **kwargs)

_agg_examples_doc = dedent(
"""
Examples
Expand Down Expand Up @@ -1535,6 +1544,12 @@ def nunique(self, dropna: bool = True) -> DataFrame:

return results

def count(self) -> DataFrame:
return cast(DataFrame, super().count())

def size(self) -> DataFrame:
return cast(DataFrame, super().size())

@Appender(DataFrame.idxmax.__doc__)
def idxmax(self, axis=0, skipna: bool = True):
axis = DataFrame._get_axis_number(axis)
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,6 @@ def all(self, skipna: bool = True):
"""
return self._bool_agg("all", skipna)

@final
@Substitution(name="groupby")
@Appender(_common_see_also)
def count(self) -> Series | DataFrame:
Expand Down Expand Up @@ -2135,7 +2134,6 @@ def sem(self, ddof: int = 1):
result.iloc[:, result_ilocs] /= np.sqrt(counts.iloc[:, count_ilocs])
return result

@final
@Substitution(name="groupby")
@Appender(_common_see_also)
def size(self) -> DataFrame | Series:
Expand Down