Skip to content

Commit f7c79be

Browse files
topper-123jreback
authored andcommitted
Added repr string for Grouper and TimeGrouper (pandas-dev#18203)
1 parent 1043a46 commit f7c79be

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
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:

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Removal of prior version deprecations/changes
100100
- The ``levels`` and ``labels`` attributes of a ``MultiIndex`` can no longer be set directly (:issue:`4039`).
101101
- ``pd.tseries.util.pivot_annual`` has been removed (deprecated since v0.19). Use ``pivot_table`` instead (:issue:`18370`)
102102
- ``pd.tseries.util.isleapyear`` has been removed (deprecated since v0.19). Use ``.is_leap_year`` property in Datetime-likes instead (:issue:`18370`)
103-
- ``pd.ordered_merge`` has been removed (deprecated since v0.19). Use ``pd..merge_ordered`` instead (:issue:`18459`)
103+
- ``pd.ordered_merge`` has been removed (deprecated since v0.19). Use ``pd.merge_ordered`` instead (:issue:`18459`)
104104

105105
.. _whatsnew_0220.performance:
106106

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
@@ -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)