Skip to content

Commit 526f33c

Browse files
committed
Merge pull request pandas-dev#9008 from jreback/grouper
BUG: Bug in using a pd.Grouper(key=...) with no level/axis or level only (GH8795, GH8866)
2 parents 71c06b8 + 7059703 commit 526f33c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

doc/source/whatsnew/v0.15.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Bug Fixes
9898

9999
- Bug in Timestamp-Timestamp not returning a Timedelta type and datelike-datelike ops with timezones (:issue:`8865`)
100100
- Made consistent a timezone mismatch exception (either tz operated with None or incompatible timezone), will now return ``TypeError`` rather than ``ValueError`` (a couple of edge cases only), (:issue:`8865`)
101+
- Bug in using a ``pd.Grouper(key=...)`` with no level/axis or level only (:issue:`8795`, :issue:`8866`)
101102
- Report a ``TypeError`` when invalid/no paramaters are passed in a groupby (:issue:`8015`)
102103
- Bug in packaging pandas with ``py2app/cx_Freeze`` (:issue:`8602`, :issue:`8831`)
103104
- Bug in ``groupby`` signatures that didn't include \*args or \*\*kwargs (:issue:`8733`).

pandas/core/groupby.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class Grouper(object):
168168
freq : string / freqency object, defaults to None
169169
This will groupby the specified frequency if the target selection (via key or level) is
170170
a datetime-like object
171-
axis : number/name of the axis, defaults to None
171+
axis : number/name of the axis, defaults to 0
172172
sort : boolean, default to False
173173
whether to sort the resulting labels
174174
@@ -198,7 +198,7 @@ def __new__(cls, *args, **kwargs):
198198
cls = TimeGrouper
199199
return super(Grouper, cls).__new__(cls)
200200

201-
def __init__(self, key=None, level=None, freq=None, axis=None, sort=False):
201+
def __init__(self, key=None, level=None, freq=None, axis=0, sort=False):
202202
self.key=key
203203
self.level=level
204204
self.freq=freq
@@ -228,6 +228,8 @@ def _get_grouper(self, obj):
228228
"""
229229

230230
self._set_grouper(obj)
231+
self.grouper, exclusions, self.obj = _get_grouper(self.obj, [self.key], axis=self.axis,
232+
level=self.level, sort=self.sort)
231233
return self.binner, self.grouper, self.obj
232234

233235
def _set_grouper(self, obj, sort=False):

pandas/tests/test_groupby.py

+33
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,39 @@ def test_grouper_multilevel_freq(self):
373373
pd.Grouper(level=1, freq='W')]).sum()
374374
assert_frame_equal(result, expected)
375375

376+
def test_grouper_creation_bug(self):
377+
378+
# GH 8795
379+
df = DataFrame({'A':[0,0,1,1,2,2], 'B':[1,2,3,4,5,6]})
380+
g = df.groupby('A')
381+
expected = g.sum()
382+
383+
g = df.groupby(pd.Grouper(key='A'))
384+
result = g.sum()
385+
assert_frame_equal(result, expected)
386+
387+
result = g.apply(lambda x: x.sum())
388+
assert_frame_equal(result, expected)
389+
390+
g = df.groupby(pd.Grouper(key='A',axis=0))
391+
result = g.sum()
392+
assert_frame_equal(result, expected)
393+
394+
# GH8866
395+
s = Series(np.arange(8),
396+
index=pd.MultiIndex.from_product([list('ab'),
397+
range(2),
398+
date_range('20130101',periods=2)],
399+
names=['one','two','three']))
400+
result = s.groupby(pd.Grouper(level='three',freq='M')).sum()
401+
expected = Series([28],index=Index([Timestamp('2013-01-31')],freq='M',name='three'))
402+
assert_series_equal(result, expected)
403+
404+
# just specifying a level breaks
405+
result = s.groupby(pd.Grouper(level='one')).sum()
406+
expected = s.groupby(level='one').sum()
407+
assert_series_equal(result, expected)
408+
376409
def test_grouper_iter(self):
377410
self.assertEqual(sorted(self.df.groupby('A').grouper), ['bar', 'foo'])
378411

0 commit comments

Comments
 (0)