-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -837,11 +838,22 @@ def names(self): | |
|
||
@property | ||
def groupings(self) -> "List[grouper.Grouping]": | ||
codes, _, _ = self.group_info | ||
|
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the issue then len(Grouper) != ngroups right? why are you adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed changing |
||
return iter(self.groupings[0].grouper) | ||
|
||
def agg_series(self, obj: Series, func): | ||
# Caller is responsible for checking ngroups != 0 | ||
assert self.ngroups != 0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1863,6 +1863,17 @@ def test_groupby_groups_in_BaseGrouper(): | |
expected = df.groupby(["beta", "alpha"]) | ||
assert result.groups == expected.groups | ||
|
||
# GH 33132 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment.
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.