-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: ngroups and len(groups) do not equal when grouping with a list of Grouper and column label (GH26326) #26374
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 8 commits
47a1057
cad3d6b
0dba40d
bb3f6de
dc76a2d
648ddfa
3aaa15c
f8a3220
ca6eff6
c3d5de2
f8f3d8f
b8e4b34
bfdea4c
49615db
050fb89
bf48c41
af63e6e
fe0405a
ee4dffe
ea023de
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 |
---|---|---|
|
@@ -258,7 +258,12 @@ def groups(self): | |
if len(self.groupings) == 1: | ||
return self.groupings[0].groups | ||
else: | ||
to_groupby = zip(*(ping.grouper for ping in self.groupings)) | ||
def is_basegrouper(self, obj): | ||
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. this is not needed |
||
return obj.__class__.__name__ == self.__class__.__name__ | ||
to_groupby = zip(*(ping.grouper if not is_basegrouper(self, | ||
ping.grouper) | ||
else ping.grouper.groupings[0].grouper for ping | ||
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. Wouldn't this only work if the grouper was the first item? Would need something more generalizable 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. I was wondering the same. Can Groupers be nested to more than one level? 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. I think this would fail if your test was 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 problem is with the construction of the |
||
in self.groupings)) | ||
shantanu-gontia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
to_groupby = Index(to_groupby) | ||
return self.axis.groupby(to_groupby) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1736,3 +1736,18 @@ def test_groupby_multiindex_series_keys_len_equal_group_axis(): | |
expected = pd.Series([3], index=ei) | ||
|
||
assert_series_equal(result, expected) | ||
|
||
|
||
def test_groupby_groups_in_BaseGrouper(): | ||
# GH 26326 | ||
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 give a 1-line description of what this is testing 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. sure. will do so in the next commit |
||
mi = pd.MultiIndex.from_product([['A', 'B'], | ||
['C', 'D']], names=['alpha', 'beta']) | ||
df = pd.DataFrame({'foo': [1, 2, 1, 2], 'bar': [1, 2, 3, 4]}, | ||
index=mi) | ||
grp1 = df.groupby([pd.Grouper(level='alpha'), '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. can you call this result = and the nbggrp1 -> expected then do then consecutively, e.g. like
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. sure, looks cleaner that way. |
||
grp2 = df.groupby(['beta', pd.Grouper(level='alpha')]) | ||
nbggrp1 = df.groupby(['alpha', 'beta']) | ||
nbggrp2 = df.groupby(['beta', 'alpha']) | ||
|
||
assert(grp1.groups == nbggrp1.groups) | ||
assert(grp2.groups == nbggrp2.groups) |
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.
this is not user facing, can you reword a bit