Skip to content

allow args and kwargs in groupby.apply #1268

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 4 commits into from
Jun 30, 2025
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
38 changes: 28 additions & 10 deletions pandas-stubs/core/groupby/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from typing import (
Generic,
Literal,
NamedTuple,
Protocol,
TypeVar,
final,
overload,
Expand Down Expand Up @@ -208,28 +209,45 @@ class SeriesGroupBy(GroupBy[Series[S2]], Generic[S2, ByT]):

_TT = TypeVar("_TT", bound=Literal[True, False])

# ty ignore needed because of https://github.com/astral-sh/ty/issues/157#issuecomment-3017337945
class DFCallable1(Protocol[P]): # ty: ignore[invalid-argument-type]
def __call__(
self, df: DataFrame, /, *args: P.args, **kwargs: P.kwargs
) -> Scalar | list | dict: ...

class DFCallable2(Protocol[P]): # ty: ignore[invalid-argument-type]
def __call__(
self, df: DataFrame, /, *args: P.args, **kwargs: P.kwargs
) -> DataFrame | Series: ...

class DFCallable3(Protocol[P]): # ty: ignore[invalid-argument-type]
def __call__(self, df: Iterable, /, *args: P.args, **kwargs: P.kwargs) -> float: ...

class DataFrameGroupBy(GroupBy[DataFrame], Generic[ByT, _TT]):
# error: Overload 3 for "apply" will never be used because its parameters overlap overload 1
@overload # type: ignore[override]
def apply(
self,
func: Callable[[DataFrame], Scalar | list | dict],
*args,
**kwargs,
func: DFCallable1[P],
/,
*args: P.args,
**kwargs: P.kwargs,
) -> Series: ...
@overload
def apply(
self,
func: Callable[[DataFrame], Series | DataFrame],
*args,
**kwargs,
func: DFCallable2[P],
/,
*args: P.args,
**kwargs: P.kwargs,
) -> DataFrame: ...
@overload
def apply( # pyright: ignore[reportOverlappingOverload]
def apply(
self,
func: Callable[[Iterable], float],
*args,
**kwargs,
func: DFCallable3[P],
/,
*args: P.args,
**kwargs: P.kwargs,
) -> DataFrame: ...
# error: overload 1 overlaps overload 2 because of different return types
@overload
Expand Down
28 changes: 26 additions & 2 deletions tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def resample_interpolate(x: DataFrame) -> DataFrame:

check(
assert_type(
GB_DF.apply(resample_interpolate, include_groups=False),
GB_DF.apply(resample_interpolate),
DataFrame,
),
DataFrame,
Expand All @@ -286,7 +286,6 @@ def resample_interpolate_linear(x: DataFrame) -> DataFrame:
assert_type(
GB_DF.apply(
resample_interpolate_linear,
include_groups=False,
),
DataFrame,
),
Expand Down Expand Up @@ -1102,3 +1101,28 @@ def test_dataframe_value_counts() -> None:
Series,
np.int64,
)


def test_dataframe_apply_kwargs() -> None:
# GH 1266
df = DataFrame({"group": ["A", "A", "B", "B", "C"], "value": [10, 15, 10, 25, 30]})

def add_constant_to_mean(group: DataFrame, constant: int) -> DataFrame:
mean_val = group["value"].mean()
group["adjusted"] = mean_val + constant
return group

check(
assert_type(
df.groupby("group", group_keys=False)[["group", "value"]].apply(
add_constant_to_mean, constant=5
),
DataFrame,
),
DataFrame,
)
if TYPE_CHECKING_INVALID_USAGE:
df.groupby("group", group_keys=False)[["group", "value"]].apply(
add_constant_to_mean,
constant="5", # type: ignore[call-overload] # pyright: ignore[reportCallIssue, reportArgumentType]
)