Skip to content

added return types for "SeriesGroupBy" and "DataFrameGroupBy" #455

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
Nov 30, 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
4 changes: 2 additions & 2 deletions pandas-stubs/core/groupby/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class SeriesGroupBy(GroupBy, Generic[S1]):
def agg(self, func: list[AggFuncTypeBase], *args, **kwargs) -> DataFrame: ...
@overload
def agg(self, func: AggFuncTypeBase, *args, **kwargs) -> Series: ...
def transform(self, func, *args, **kwargs): ...
def transform(self, func: Callable | str, *args, **kwargs) -> Series: ...
def filter(self, func, dropna: bool = ..., *args, **kwargs): ...
def nunique(self, dropna: bool = ...) -> Series: ...
def describe(self, **kwargs) -> DataFrame: ...
Expand Down Expand Up @@ -166,7 +166,7 @@ class DataFrameGroupBy(GroupBy):
) -> DataFrame: ...
def aggregate(self, arg: AggFuncTypeFrame = ..., *args, **kwargs) -> DataFrame: ...
agg = aggregate
def transform(self, func, *args, **kwargs): ...
def transform(self, func: Callable | str, *args, **kwargs) -> DataFrame: ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should we make this a Literal?

From https://github.com/pandas-dev/pandas/blob/main/pandas/core/groupby/base.py#L60, the transformation kernels would be the ones applicable here:

transformation_kernels = Literal[
    "bfill",
    "cumcount",
    "cummax",
    "cummin",
    "cumprod",
    "cumsum",
    "diff",
    "ffill",
    "fillna",
    "ngroup",
    "pct_change",
    "rank",
    "shift",
]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to do better than Callable? Like what about Callable[Concatenate[pd.DataFrame, P], pd.DataFrame]? Will ParamSpec work in that context? If not, can we use a Protocol? I know that we can't use P.args on *args and P.kwargs on **kwargs, though that would have been a very nice feature.

I'm just thinking, if we are going from an unannotated function to an annotated function, a loose annotation doesn't really do that much good versus not having an annotation at all.

Copy link
Contributor Author

@ramvikrams ramvikrams Nov 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do this , but I think it would be better to ask Dr Irv about adding it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather be incremental in terms of PR's where we could specify the Literal values (there are more than just the ones you listed), as well as making the func argument more specific. @gandhis1 feel free to create an issue indicating that we could improve the typing on groupby.transform by being more specific about the allowed argument combinations. It also would require a lot more tests.

Focus of this PR was just to get the return types right, and do a slight improvement in the func argument.

def filter(
self, func: Callable, dropna: bool = ..., *args, **kwargs
) -> DataFrame: ...
Expand Down
26 changes: 26 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,3 +2063,29 @@ def test_setitem_none() -> None:
sb = pd.Series([1, 2, 3], dtype=int)
sb.loc["y"] = None
sb.iloc[0] = None


def test_groupby_and_transform() -> None:
df = pd.DataFrame(
{
"A": ["foo", "bar", "foo", "bar", "foo", "bar"],
"B": ["one", "one", "two", "three", "two", "two"],
"C": [1, 5, 5, 2, 5, 5],
"D": [2.0, 5.0, 8.0, 1.0, 2.0, 9.0],
}
)
ser = pd.Series(
[390.0, 350.0, 30.0, 20.0],
index=["Falcon", "Falcon", "Parrot", "Parrot"],
name="Max Speed",
)
grouped = df.groupby("A")[["C", "D"]]
grouped1 = ser.groupby(ser > 100)
c1 = grouped.transform("sum")
c2 = grouped.transform(lambda x: (x - x.mean()) / x.std())
c3 = grouped1.transform("cumsum")
c4 = grouped1.transform(lambda x: x.max() - x.min())
check(assert_type(c1, pd.DataFrame), pd.DataFrame)
check(assert_type(c2, pd.DataFrame), pd.DataFrame)
check(assert_type(c3, pd.Series), pd.Series)
check(assert_type(c4, pd.Series), pd.Series)