Skip to content

Bug: Error when key-only Grouper is passed to groupby in a list (GH14334) #14342

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
wants to merge 6 commits into from
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.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Bug Fixes
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
- Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`)
- Bug in ``df.groupby`` where ``TypeError`` raised when ``pd.Grouper(key=...)`` is passed in a list (:issue:`14334`)

- Bug in ``pd.pivot_table`` may raise ``TypeError`` or ``ValueError`` when ``index`` or ``columns``
is not scalar and ``values`` is not specified (:issue:`14380`)
10 changes: 8 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,7 +2208,10 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
index._get_grouper_for_level(self.grouper, level)

else:
if isinstance(self.grouper, (list, tuple)):
if self.grouper is None and self.name is not None:
self.grouper = self.obj[self.name]

elif isinstance(self.grouper, (list, tuple)):
self.grouper = com._asarray_tuplesafe(self.grouper)

# a passed Categorical
Expand Down Expand Up @@ -2448,7 +2451,10 @@ def is_in_obj(gpr):
elif is_in_axis(gpr): # df.groupby('name')
in_axis, name, gpr = True, gpr, obj[gpr]
exclusions.append(name)

elif isinstance(gpr, Grouper) and gpr.key is not None:
# Add key to exclusions
exclusions.append(gpr.key)
in_axis, name = False, None
else:
in_axis, name = False, None

Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,36 @@ def test_grouper_creation_bug(self):
result = g.sum()
assert_frame_equal(result, expected)

# GH14334
# pd.Grouper(key=...) may be passed in a list
df = DataFrame({'A': [0, 0, 0, 1, 1, 1],
'B': [1, 1, 2, 2, 3, 3],
'C': [1, 2, 3, 4, 5, 6]})
# Group by single column
expected = df.groupby('A').sum()
g = df.groupby([pd.Grouper(key='A')])
Copy link
Contributor

Choose a reason for hiding this comment

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

add a 1-liner explaining the bug

result = g.sum()
assert_frame_equal(result, expected)

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 add a test with multiple pd.Grouper passed (both with keys) and then a string and a Grouper (unless these combinations already exist)

# Group by two columns
# using a combination of strings and Grouper objects
expected = df.groupby(['A', 'B']).sum()

# Group with two Grouper objects
g = df.groupby([pd.Grouper(key='A'), pd.Grouper(key='B')])
result = g.sum()
assert_frame_equal(result, expected)

# Group with a string and a Grouper object
g = df.groupby(['A', pd.Grouper(key='B')])
result = g.sum()
assert_frame_equal(result, expected)

# Group with a Grouper object and a string
g = df.groupby([pd.Grouper(key='A'), 'B'])
result = g.sum()
assert_frame_equal(result, expected)

# GH8866
s = Series(np.arange(8, dtype='int64'),
index=pd.MultiIndex.from_product(
Expand Down