Skip to content

Allow sequence in groupby level #837

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
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 8 additions & 8 deletions pandas-stubs/core/frame.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: Scalar,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1022,7 +1022,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: DatetimeIndex,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1035,7 +1035,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: TimedeltaIndex,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1048,7 +1048,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: PeriodIndex,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1061,7 +1061,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: IntervalIndex[IntervalT],
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1074,7 +1074,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: MultiIndex | GroupByObjectNonScalar | None = ...,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1087,7 +1087,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: Series[SeriesByT],
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -1100,7 +1100,7 @@ class DataFrame(NDFrame, OpsMixin):
self,
by: CategoricalIndex | Index | Series,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand Down
17 changes: 9 additions & 8 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ from pandas._typing import (
HashableT3,
IgnoreRaise,
IndexingInt,
IndexLabel,
IntDtypeArg,
InterpolateOptions,
IntervalClosedType,
Expand Down Expand Up @@ -547,7 +548,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: Scalar,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -560,7 +561,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: DatetimeIndex,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -573,7 +574,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: TimedeltaIndex,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -586,7 +587,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: PeriodIndex,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -599,7 +600,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: IntervalIndex[IntervalT],
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -612,7 +613,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: MultiIndex | GroupByObjectNonScalar = ...,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -625,7 +626,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: Series[SeriesByT],
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand All @@ -638,7 +639,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
self,
by: CategoricalIndex | Index | Series,
axis: AxisIndex = ...,
level: Level | None = ...,
level: IndexLabel | None = ...,
as_index: _bool = ...,
sort: _bool = ...,
group_keys: _bool = ...,
Expand Down
12 changes: 12 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,18 @@ def test_types_groupby_iter() -> None:
)


def test_types_groupby_level() -> None:
# GH 836
data = {
"col1": [0, 0, 0],
"col2": [0, 1, 0],
"col3": [1, 2, 3],
"col4": [1, 2, 3],
}
df = pd.DataFrame(data=data).set_index(["col1", "col2", "col3"])
df.groupby(level=["col1", "col2"]).sum()


def test_types_merge() -> None:
df = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [3, 4, 5]})
df2 = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [0, 1, 0]})
Expand Down
9 changes: 9 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,15 @@ def test_types_max() -> None:
s.max(skipna=False)


def test_types_groupby_level() -> None:
# GH 836
index = pd.MultiIndex.from_tuples(
[(0, 0, 1), (0, 1, 2), (0, 0, 3)], names=["col1", "col2", "col3"]
)
s = pd.Series([1, 2, 3], index=index)
s.groupby(level=["col1", "col2"]).sum()


def test_types_quantile() -> None:
s = pd.Series([1, 2, 3, 10])
s.quantile([0.25, 0.5])
Expand Down