-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
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 16 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 |
---|---|---|
|
@@ -150,6 +150,15 @@ def _get_splitter(self, data, axis=0): | |
comp_ids, _, ngroups = self.group_info | ||
return get_splitter(data, comp_ids, ngroups, axis=axis) | ||
|
||
def _get_grouper(self): | ||
""" | ||
We are a grouper as part of another's groupings. | ||
|
||
We have a specific method of grouping, so cannot | ||
convert to a Index for our grouper. | ||
""" | ||
return self.groupings[0].grouper | ||
|
||
def _get_group_keys(self): | ||
if len(self.groupings) == 1: | ||
return self.levels[0] | ||
|
@@ -258,6 +267,8 @@ def groups(self): | |
if len(self.groupings) == 1: | ||
return self.groupings[0].groups | ||
else: | ||
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 for ping in self.groupings)) | ||
to_groupby = Index(to_groupby) | ||
return self.axis.groupby(to_groupby) | ||
|
@@ -707,6 +718,15 @@ def groups(self): | |
def nkeys(self): | ||
return 1 | ||
|
||
def _get_grouper(self): | ||
""" | ||
We are a grouper as part of another's groupings. | ||
|
||
We have a specific method of grouping, so cannot | ||
convert to a Index for our grouper. | ||
""" | ||
return self | ||
|
||
def get_iterator(self, data, axis=0): | ||
""" | ||
Groupby iterator | ||
|
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) |
Uh oh!
There was an error while loading. Please reload this page.
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.
incorrect groups -> incorrect groups when using
.groups
accessorThere 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.
will do so in the next commit.