Skip to content

BUG: DataFrame.groupby() interprets tuple as list of keys #17996

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Bug Fixes
~~~~~~~~~

- Bug in ``pd.read_msgpack()`` with a non existent file is passed in Python 2 (:issue:`15296`)
- Bug in ``DataFrame.groupby`` where key as tuple in a ``MultiIndex`` were interpreted as a list of keys (:issue:`17979`)

Conversion
^^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,7 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,

"""
group_axis = obj._get_axis(axis)
is_axis_multiindex = isinstance(obj._info_axis, MultiIndex)

# validate that the passed single level is compatible with the passed
# axis of the object
Expand Down Expand Up @@ -2765,7 +2766,9 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
elif isinstance(key, BaseGrouper):
return key, [], obj

if not isinstance(key, (tuple, list)):
# when MultiIndex, allow tuple to be a key
if not isinstance(key, (tuple, list)) or \
(isinstance(key, tuple) and is_axis_multiindex):
keys = [key]
match_axis_length = False
else:
Expand Down Expand Up @@ -2869,7 +2872,6 @@ def is_in_obj(gpr):

# create the internals grouper
grouper = BaseGrouper(group_axis, groupings, sort=sort, mutated=mutated)

return grouper, exclusions, obj


Expand Down
Loading