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 1 commit
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 @@ -406,7 +406,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 a :class:`BaseGrouper` object with another :class:`BaseGrouper` as part of its :class:`Groupings` would return incorrect set of groups (:issue:`26326`)
Copy link
Member

Choose a reason for hiding this comment

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

Can you construct a message that is more user facing? These items aren't part of the API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

User facing as in more user friendly?


Reshaping
^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ def groups(self):
if len(self.groupings) == 1:
return self.groupings[0].groups
else:
to_groupby = zip(*(ping.grouper for ping in self.groupings))
to_groupby = zip(*(ping.grouper if not isinstance(ping.grouper,
self.__class__)
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
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1737,3 +1737,15 @@ 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():
# https://github.com/pandas-dev/pandas/issues/26326
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# https://github.com/pandas-dev/pandas/issues/26326
# GH 26326

m_index = pd.MultiIndex.from_product([['A', 'B'],
Copy link
Member

Choose a reason for hiding this comment

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

Use mi instead of m_index

['C', 'D']], names=['alpha', 'beta'])
df_sample = pd.DataFrame({'foo': [1, 2, 1, 2], 'bar': [1, 2, 3, 4]},
Copy link
Member

Choose a reason for hiding this comment

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

df instead of df_sample

index=m_index)
dfGBY_BaseGrouper = df_sample.groupby([pd.Grouper(level='alpha'), 'beta'])
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just grp1 and grp2 for better readability here and line below

dfGBY_noBaseGrouper = df_sample.groupby(['alpha', 'beta'])

assert(dfGBY_BaseGrouper.groups == dfGBY_noBaseGrouper.groups)