Skip to content

Commit 1a4747b

Browse files
author
tp
committed
Added repr string for Grouper and TimeGrouper
1 parent 9c9a09f commit 1a4747b

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

doc/source/whatsnew/v0.21.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Other Enhancements
2222
^^^^^^^^^^^^^^^^^^
2323

2424
- :meth:`Timestamp.timestamp` is now available in Python 2.7. (:issue:`17329`)
25-
-
25+
- :class:`Grouper` and :class:`TimeGrouper` now have a friendly repr output (:issue:`18203`).
2626
-
2727

2828
.. _whatsnew_0211.deprecations:

pandas/core/groupby.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,13 @@ class Grouper(object):
206206
sort : boolean, default to False
207207
whether to sort the resulting labels
208208
209-
additional kwargs to control time-like groupers (when freq is passed)
209+
additional kwargs to control time-like groupers (when ``freq`` is passed)
210210
211-
closed : closed end of interval; left or right
212-
label : interval boundary to use for labeling; left or right
211+
closed : closed end of interval; 'left' or 'right'
212+
label : interval boundary to use for labeling; 'left' or 'right'
213213
convention : {'start', 'end', 'e', 's'}
214214
If grouper is PeriodIndex
215+
base, loffset
215216
216217
Returns
217218
-------
@@ -233,6 +234,7 @@ class Grouper(object):
233234
234235
>>> df.groupby(Grouper(level='date', freq='60s', axis=1))
235236
"""
237+
_attributes = ('key', 'level', 'freq', 'axis', 'sort')
236238

237239
def __new__(cls, *args, **kwargs):
238240
if kwargs.get('freq') is not None:
@@ -333,6 +335,14 @@ def _set_grouper(self, obj, sort=False):
333335
def groups(self):
334336
return self.grouper.groups
335337

338+
def __repr__(self):
339+
attrs_list = ["{}={!r}".format(attr_name, getattr(self, attr_name))
340+
for attr_name in self._attributes
341+
if getattr(self, attr_name) is not None]
342+
attrs = ", ".join(attrs_list)
343+
cls_name = self.__class__.__name__
344+
return "{}({})".format(cls_name, attrs)
345+
336346

337347
class GroupByPlot(PandasObject):
338348
"""

pandas/core/resample.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,10 @@ class TimeGrouper(Grouper):
10251025
Use begin, end, nperiods to generate intervals that cannot be derived
10261026
directly from the associated object
10271027
"""
1028-
1028+
_attributes = Grouper._attributes + ('closed', 'label', 'how',
1029+
'nperiods', 'fill_method', 'limit',
1030+
'loffset', 'kind', 'convention',
1031+
'base')
10291032
def __init__(self, freq='Min', closed=None, label=None, how='mean',
10301033
nperiods=None, axis=0,
10311034
fill_method=None, limit=None, loffset=None, kind=None,

pandas/tests/groupby/test_groupby.py

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@
2525
from .common import MixIn
2626

2727

28+
class TestGrouper(object):
29+
30+
def test_repr(self):
31+
# GH18203
32+
result = repr(pd.Grouper(key='A', level='B'))
33+
expected = "Grouper(key='A', level='B', axis=0, sort=False)"
34+
assert result == expected
35+
36+
2837
class TestGroupBy(MixIn):
2938

3039
def test_basic(self):

pandas/tests/test_resample.py

+8
Original file line numberDiff line numberDiff line change
@@ -3416,3 +3416,11 @@ def test_aggregate_with_nat(self):
34163416

34173417
# if NaT is included, 'var', 'std', 'mean', 'first','last'
34183418
# and 'nth' doesn't work yet
3419+
3420+
def test_repr(self):
3421+
# GH18203
3422+
result = repr(TimeGrouper(key='A', freq='H'))
3423+
expected = ("TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True, "
3424+
"closed='left', label='left', how='mean', "
3425+
"convention='e', base=0)")
3426+
assert result == expected

0 commit comments

Comments
 (0)