Skip to content

More specific types for GroupBy.apply. #177

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 3 commits into from
Aug 3, 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
11 changes: 7 additions & 4 deletions pandas-stubs/core/groupby/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class SeriesGroupBy(GroupBy):
def nlargest(self, n: int = ..., keep: str = ...) -> Series[S1]: ...
def nsmallest(self, n: int = ..., keep: str = ...) -> Series[S1]: ...
def nth(self, n: int | Sequence[int], dropna: str | None = ...) -> Series[S1]: ...
def sum(self, **kwargs) -> Series[S1]: ...

class _DataFrameGroupByScalar(DataFrameGroupBy):
def __iter__(self) -> Iterator[tuple[Scalar, DataFrame]]: ...
Expand All @@ -110,17 +111,19 @@ class _DataFrameGroupByNonScalar(DataFrameGroupBy):
class DataFrameGroupBy(GroupBy):
def any(self, skipna: bool = ...) -> DataFrame: ...
def all(self, skipna: bool = ...) -> DataFrame: ...
# mypy sees the two overloads as overlapping
# mypy and pyright see these overloads as overlapping
@overload
def apply( # type: ignore[misc]
self, func: Callable[[DataFrame], Series | Scalar], *args, **kwargs
self, func: Callable[[DataFrame], Scalar | list | dict], *args, **kwargs
) -> Series: ...
@overload
def apply( # type: ignore[misc]
self, func: Callable[[Iterable], Series | Scalar], *args, **kwargs
self, func: Callable[[DataFrame], Series | DataFrame], *args, **kwargs
) -> DataFrame: ...
@overload
def apply(self, func: Callable, *args, **kwargs) -> DataFrame | Series: ...
def apply( # type: ignore[misc]
self, func: Callable[[Iterable], float], *args, **kwargs
) -> DataFrame: ...
@overload
def aggregate(self, arg: str, *args, **kwargs) -> DataFrame: ...
@overload
Expand Down
23 changes: 21 additions & 2 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,13 +1346,32 @@ def test_groupby_apply() -> None:
# GH 167
df = pd.DataFrame({"col1": [1, 2, 3], "col2": [4, 5, 6]})

def summean(x: pd.DataFrame) -> float:
def sum_mean(x: pd.DataFrame) -> float:
return x.sum().mean()

check(assert_type(df.groupby("col1").apply(summean), pd.Series), pd.Series)
check(assert_type(df.groupby("col1").apply(sum_mean), pd.Series), pd.Series)

lfunc: Callable[[pd.DataFrame], float] = lambda x: x.sum().mean()
check(
assert_type(df.groupby("col1").apply(lfunc), pd.Series),
pd.Series,
)

def sum_to_list(x: pd.DataFrame) -> list:
return x.sum().tolist()

check(assert_type(df.groupby("col1").apply(sum_to_list), pd.Series), pd.Series)

def sum_to_series(x: pd.DataFrame) -> pd.Series:
return x.sum()

check(
assert_type(df.groupby("col1").apply(sum_to_series), pd.DataFrame), pd.DataFrame
)

def sample_to_df(x: pd.DataFrame) -> pd.DataFrame:
return x.sample()

check(
assert_type(df.groupby("col1").apply(sample_to_df), pd.DataFrame), pd.DataFrame
)