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 16 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 @@ -444,9 +444,9 @@ Groupby/Resample/Rolling
- 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.SeriesGroupBy.transform` where transforming an empty group would raise error (:issue:`26208`)
- Bug in :meth:`pandas.core.frame.DataFrame.groupby` where passing a :class:`pandas.core.groupby.grouper.Grouper` would return incorrect groups (:issue:`26326`)
Copy link
Contributor

@jreback jreback May 19, 2019

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 accessor

Copy link
Contributor Author

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.

- Bug in :meth:`pandas.core.groupby.GroupBy.agg` where incorrect results are returned for uint64 columns. (:issue:`26310`)


Reshaping
^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
if self.name is None:
self.name = grouper.result_index.name
self.obj = self.grouper.obj
self.grouper = grouper
self.grouper = grouper._get_grouper()

else:
if self.grouper is None and self.name is not None:
Expand Down
20 changes: 20 additions & 0 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -258,6 +267,8 @@ def groups(self):
if len(self.groupings) == 1:
return self.groupings[0].groups
else:
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 for ping in self.groupings))
to_groupby = Index(to_groupby)
return self.axis.groupby(to_groupby)
Expand Down Expand Up @@ -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
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)