Skip to content

BUG: fix ngroups and len(groups) inconsistency when using [Grouper(freq=)] (GH33132) #33135

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

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 13 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from pandas.core.dtypes.missing import _maybe_fill, isna

import pandas.core.algorithms as algorithms
from pandas.core.arrays import Categorical
from pandas.core.base import SelectionMixin
import pandas.core.common as com
from pandas.core.frame import DataFrame
Expand Down Expand Up @@ -837,11 +838,22 @@ def names(self):

@property
def groupings(self) -> "List[grouper.Grouping]":
codes, _, _ = self.group_info
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not really sure what you are hoping to accomplish here.


if self.indexer is not None and len(self.indexer) != len(codes):
groupers = self.levels
else:
groupers = [self.result_index._constructor(
Categorical.from_codes(self.codes_info, self.result_index))]

return [
grouper.Grouping(lvl, lvl, in_axis=False, level=None, name=name)
for lvl, name in zip(self.levels, self.names)
for lvl, name in zip(groupers, self.names)
]

def __iter__(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

the issue then len(Grouper) != ngroups right? why are you adding __iter__? rather than for example overriding __len__?

Copy link
Author

Choose a reason for hiding this comment

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

Indeed changing __iter__ was a bad idea since this changes the behavior of resample. Anyway, iterating over a nested BinGrouper is somewhat part of the problem (check #33132 (comment) plz). #33303 probably has a better approach to resolve this.

return iter(self.groupings[0].grouper)

def agg_series(self, obj: Series, func):
# Caller is responsible for checking ngroups != 0
assert self.ngroups != 0
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,17 @@ def test_groupby_groups_in_BaseGrouper():
expected = df.groupby(["beta", "alpha"])
assert result.groups == expected.groups

# GH 33132
Copy link
Member

Choose a reason for hiding this comment

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

can you make this a separate test

# Test if DataFrame grouped with a pandas.Grouper and freq param has correct groups
mi = pd.MultiIndex.from_product([date_range(datetime.today(), periods=2),
["C", "D"]], names=["alpha", "beta"])
df = pd.DataFrame({"foo": [1, 2, 1, 2], "bar": [1, 2, 3, 4]}, index=mi)
result = df.groupby(["beta", pd.Grouper(level="alpha", freq='D')])
assert result.ngroups == len(result)

result = df.groupby([pd.Grouper(level="alpha", freq='D'), "beta"])
Copy link
Member

Choose a reason for hiding this comment

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

linter is going to want double-quotes on 'D'

assert result.ngroups == len(result)


@pytest.mark.parametrize("group_name", ["x", ["x"]])
def test_groupby_axis_1(group_name):
Expand Down