Skip to content

Commit 4852008

Browse files
Jon M. Measejreback
Jon M. Mease
authored andcommitted
Bug: Error when key-only Grouper is passed to groupby in a list (GH14334)
closes #14334 Author: Jon M. Mease <[email protected]> Closes #14342 from jmmease/bug_14334 and squashes the following commits: 5e96797 [Jon M. Mease] Add tests for grouping on two columns cee5ce6 [Jon M. Mease] Added bug description to new test case f9ef05b [Jon M. Mease] Moved whatsnew to 0.19.1 and clarified description 14a4ae6 [Jon M. Mease] Added whatsnew for GH 14334 9805c30 [Jon M. Mease] Fix for GH 14334 dfd3e09 [Jon M. Mease] Added test case for GH 14334
1 parent 6ff53c2 commit 4852008

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

doc/source/whatsnew/v0.19.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Bug Fixes
6464
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)
6565
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
6666
- Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`)
67+
- Bug in ``df.groupby`` where ``TypeError`` raised when ``pd.Grouper(key=...)`` is passed in a list (:issue:`14334`)
6768

6869

6970

pandas/core/groupby.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,10 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
22082208
index._get_grouper_for_level(self.grouper, level)
22092209

22102210
else:
2211-
if isinstance(self.grouper, (list, tuple)):
2211+
if self.grouper is None and self.name is not None:
2212+
self.grouper = self.obj[self.name]
2213+
2214+
elif isinstance(self.grouper, (list, tuple)):
22122215
self.grouper = com._asarray_tuplesafe(self.grouper)
22132216

22142217
# a passed Categorical
@@ -2448,7 +2451,10 @@ def is_in_obj(gpr):
24482451
elif is_in_axis(gpr): # df.groupby('name')
24492452
in_axis, name, gpr = True, gpr, obj[gpr]
24502453
exclusions.append(name)
2451-
2454+
elif isinstance(gpr, Grouper) and gpr.key is not None:
2455+
# Add key to exclusions
2456+
exclusions.append(gpr.key)
2457+
in_axis, name = False, None
24522458
else:
24532459
in_axis, name = False, None
24542460

pandas/tests/test_groupby.py

+30
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,36 @@ def test_grouper_creation_bug(self):
442442
result = g.sum()
443443
assert_frame_equal(result, expected)
444444

445+
# GH14334
446+
# pd.Grouper(key=...) may be passed in a list
447+
df = DataFrame({'A': [0, 0, 0, 1, 1, 1],
448+
'B': [1, 1, 2, 2, 3, 3],
449+
'C': [1, 2, 3, 4, 5, 6]})
450+
# Group by single column
451+
expected = df.groupby('A').sum()
452+
g = df.groupby([pd.Grouper(key='A')])
453+
result = g.sum()
454+
assert_frame_equal(result, expected)
455+
456+
# Group by two columns
457+
# using a combination of strings and Grouper objects
458+
expected = df.groupby(['A', 'B']).sum()
459+
460+
# Group with two Grouper objects
461+
g = df.groupby([pd.Grouper(key='A'), pd.Grouper(key='B')])
462+
result = g.sum()
463+
assert_frame_equal(result, expected)
464+
465+
# Group with a string and a Grouper object
466+
g = df.groupby(['A', pd.Grouper(key='B')])
467+
result = g.sum()
468+
assert_frame_equal(result, expected)
469+
470+
# Group with a Grouper object and a string
471+
g = df.groupby([pd.Grouper(key='A'), 'B'])
472+
result = g.sum()
473+
assert_frame_equal(result, expected)
474+
445475
# GH8866
446476
s = Series(np.arange(8, dtype='int64'),
447477
index=pd.MultiIndex.from_product(

0 commit comments

Comments
 (0)