Skip to content

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

Merged
merged 20 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
47a1057
BUG: ngroups and len(groups) do not equal when grouping with a list o…
shantanu-gontia May 13, 2019
cad3d6b
Sorted Imports in test_groupby.py
shantanu-gontia May 13, 2019
0dba40d
Updated test and edited bugfix message
shantanu-gontia May 13, 2019
bb3f6de
Merge remote-tracking branch 'upstream/master' into bug26326
shantanu-gontia May 13, 2019
dc76a2d
Merge remote-tracking branch 'upstream/master' into bug26326
shantanu-gontia May 14, 2019
648ddfa
Updated method to not check for subclasses
shantanu-gontia May 14, 2019
3aaa15c
Merge remote-tracking branch 'upstream/master' into bug26326
shantanu-gontia May 14, 2019
f8a3220
fixed under indent on line 264
shantanu-gontia May 14, 2019
ca6eff6
Updated release file
shantanu-gontia May 16, 2019
c3d5de2
Merge branch 'master' into bug26326
shantanu-gontia May 16, 2019
f8f3d8f
patched solution with modification of BaseGrouper
shantanu-gontia May 16, 2019
b8e4b34
updated whatsnew file with better wording
shantanu-gontia May 16, 2019
bfdea4c
fixed whatsnew release note message
shantanu-gontia May 16, 2019
49615db
added issue number to message
shantanu-gontia May 16, 2019
050fb89
fixed linting
shantanu-gontia May 16, 2019
bf48c41
Merge branch 'master' into bug26326
shantanu-gontia May 19, 2019
af63e6e
fixed release note msg
shantanu-gontia May 19, 2019
fe0405a
Added test message, refactor test code
shantanu-gontia May 19, 2019
ee4dffe
remove extraneous function
shantanu-gontia May 19, 2019
ea023de
update release note msg
shantanu-gontia May 19, 2019
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.groupby.GroupBy.idxmax` and :meth:`pandas.core.groupby.GroupBy.idxmin` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`)
- Bug in :meth:`pandas.core.groupby.GroupBy.cumsum`, :meth:`pandas.core.groupby.GroupBy.cumprod`, :meth:`pandas.core.groupby.GroupBy.cummin` and :meth:`pandas.core.groupby.GroupBy.cummax` with categorical column having absent categories, would return incorrect result or segfault (:issue:`16771`)
- Bug in :meth:`pandas.core.groupby.GroupBy.nth` where NA values in the grouping would return incorrect results (:issue:`26011`)

- Bug in :meth:`pandas.core.groupby.ops.BaseGrouper.groups` in which creating a :class:`GroupBy` object with a key of type :class:`Grouper` would result in producing incorrect groups (:issue:`26326`)
Copy link
Contributor

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


Reshaping
^^^^^^^^^
Expand Down
7 changes: 6 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

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

I think this would fail if your test was .groupby(['beta', pd.Grouper(level='alpha')]) instead of .groupby([pd.Grouper(level='alpha'), 'beta'])

Copy link
Contributor Author

@shantanu-gontia shantanu-gontia May 13, 2019

Choose a reason for hiding this comment

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

The problem is with the construction of the Groupings. The [0] indexes the sole BaseGrouper created inside the grouping due to the _get_grouper method. The order will be maintained. I have added another assertion to check this and it passed.

in self.groupings))
to_groupby = Index(to_groupby)
return self.axis.groupby(to_groupby)

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

can you give a 1-line description of what this is testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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'])
Copy link
Contributor

Choose a reason for hiding this comment

The 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

result = df.groupby(....)
expected = ....

assert result.groups == expected.groups

result = df......
expected = ....

assert

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)