Skip to content

GH203 Split groupby with as_index (temptative) #1014

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 9 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 18 additions & 3 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ from pandas import (
)
from pandas.core.arraylike import OpsMixin
from pandas.core.generic import NDFrame
from pandas.core.groupby.generic import DataFrameGroupBy
from pandas.core.groupby.generic import (
DataFrameGroupBy,
SeriesGroupBy,
)
from pandas.core.groupby.grouper import Grouper
from pandas.core.indexers import BaseIndexer
from pandas.core.indexes.base import Index
Expand Down Expand Up @@ -1052,18 +1055,30 @@ class DataFrame(NDFrame, OpsMixin):
errors: IgnoreRaise = ...,
) -> None: ...
@overload
def groupby(
def groupby( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
self,
by: Scalar,
axis: AxisIndex | NoDefault = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
as_index: Literal[False] = ...,
sort: _bool = ...,
group_keys: _bool = ...,
observed: _bool | NoDefault = ...,
dropna: _bool = ...,
) -> DataFrameGroupBy[Scalar]: ...
@overload
def groupby(
self,
by: Scalar,
axis: AxisIndex | NoDefault = ...,
level: IndexLabel | None = ...,
as_index: Literal[True] = True,
sort: _bool = ...,
group_keys: _bool = ...,
observed: _bool | NoDefault = ...,
dropna: _bool = ...,
) -> SeriesGroupBy: ...
@overload
def groupby(
self,
by: DatetimeIndex,
Expand Down
2 changes: 1 addition & 1 deletion pandas-stubs/core/groupby/groupby.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class GroupBy(BaseGroupBy[NDFrameT]):
@overload
def size(self: GroupBy[Series]) -> Series[int]: ...
@overload # return type depends on `as_index` for dataframe groupby
def size(self: GroupBy[DataFrame]) -> DataFrame | Series[int]: ...
def size(self: GroupBy[DataFrame]) -> DataFrame: ...
@final
def sum(
self,
Expand Down
20 changes: 19 additions & 1 deletion tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,24 @@ def test_types_pivot_table() -> None:
)


def test_types_groupby_as_index() -> None:
df = pd.DataFrame({"a": [1, 2, 3]})
check(
assert_type(
df.groupby("a", as_index=False).size(),
pd.DataFrame,
),
pd.DataFrame,
)
check(
assert_type(
df.groupby("a", as_index=True).size(),
"pd.Series[int]",
),
pd.Series,
)


def test_types_groupby() -> None:
df = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [3, 4, 5], "col3": [0, 1, 0]})
df.index.name = "ind"
Expand All @@ -1048,7 +1066,7 @@ def test_types_groupby() -> None:

df1: pd.DataFrame = df.groupby(by="col1").agg("sum")
df2: pd.DataFrame = df.groupby(level="ind").aggregate("sum")
df3: pd.DataFrame = df.groupby(by="col1", sort=False, as_index=True).transform(
df3: pd.Series = df.groupby(by="col1", sort=False, as_index=True).transform(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given the nature of this change, can you change this test to use the check(assert_type(... pattern?

We have old tests that haven't been converted - this is a good opportunity to convert them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, I will tackle those once we agree on the potential solution so I can do one thing at a time not to confuse the amount of code to review too much.

lambda x: x.max()
)
df4: pd.DataFrame = df.groupby(by=["col1", "col2"]).count()
Expand Down
Loading