Skip to content

Commit 8f09619

Browse files
topper-123TomAugspurger
authored andcommitted
Added repr string for Grouper and TimeGrouper (#18203)
(cherry picked from commit f7c79be)
1 parent d25a8a1 commit 8f09619

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
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

+7-12
Original file line numberDiff line numberDiff line change
@@ -1014,22 +1014,18 @@ class TimeGrouper(Grouper):
10141014
Parameters
10151015
----------
10161016
freq : pandas date offset or offset alias for identifying bin edges
1017-
closed : closed end of interval; left or right
1018-
label : interval boundary to use for labeling; left or right
1019-
nperiods : optional, integer
1017+
closed : closed end of interval; 'left' or 'right'
1018+
label : interval boundary to use for labeling; 'left' or 'right'
10201019
convention : {'start', 'end', 'e', 's'}
10211020
If axis is PeriodIndex
1022-
1023-
Notes
1024-
-----
1025-
Use begin, end, nperiods to generate intervals that cannot be derived
1026-
directly from the associated object
10271021
"""
1022+
_attributes = Grouper._attributes + ('closed', 'label', 'how',
1023+
'loffset', 'kind', 'convention',
1024+
'base')
10281025

10291026
def __init__(self, freq='Min', closed=None, label=None, how='mean',
1030-
nperiods=None, axis=0,
1031-
fill_method=None, limit=None, loffset=None, kind=None,
1032-
convention=None, base=0, **kwargs):
1027+
axis=0, fill_method=None, limit=None, loffset=None,
1028+
kind=None, convention=None, base=0, **kwargs):
10331029
freq = to_offset(freq)
10341030

10351031
end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'])
@@ -1048,7 +1044,6 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
10481044

10491045
self.closed = closed
10501046
self.label = label
1051-
self.nperiods = nperiods
10521047
self.kind = kind
10531048

10541049
self.convention = convention or 'E'

pandas/tests/groupby/test_groupby.py

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
from .common import MixIn
2929

3030

31+
class TestGrouper(object):
32+
33+
def test_repr(self):
34+
# GH18203
35+
result = repr(pd.Grouper(key='A', level='B'))
36+
expected = "Grouper(key='A', level='B', axis=0, sort=False)"
37+
assert result == expected
38+
39+
3140
class TestGroupBy(MixIn):
3241

3342
def test_basic(self):

pandas/tests/test_resample.py

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

34293429
# if NaT is included, 'var', 'std', 'mean', 'first','last'
34303430
# and 'nth' doesn't work yet
3431+
3432+
def test_repr(self):
3433+
# GH18203
3434+
result = repr(TimeGrouper(key='A', freq='H'))
3435+
expected = ("TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True, "
3436+
"closed='left', label='left', how='mean', "
3437+
"convention='e', base=0)")
3438+
assert result == expected

0 commit comments

Comments
 (0)