Skip to content

Added repr string for Grouper and TimeGrouper #18203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.21.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Other Enhancements
^^^^^^^^^^^^^^^^^^

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

.. _whatsnew_0211.deprecations:
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Removal of prior version deprecations/changes
- The ``levels`` and ``labels`` attributes of a ``MultiIndex`` can no longer be set directly (:issue:`4039`).
- ``pd.tseries.util.pivot_annual`` has been removed (deprecated since v0.19). Use ``pivot_table`` instead (:issue:`18370`)
- ``pd.tseries.util.isleapyear`` has been removed (deprecated since v0.19). Use ``.is_leap_year`` property in Datetime-likes instead (:issue:`18370`)
- ``pd.ordered_merge`` has been removed (deprecated since v0.19). Use ``pd..merge_ordered`` instead (:issue:`18459`)
- ``pd.ordered_merge`` has been removed (deprecated since v0.19). Use ``pd.merge_ordered`` instead (:issue:`18459`)

.. _whatsnew_0220.performance:

Expand Down
16 changes: 13 additions & 3 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ class Grouper(object):
sort : boolean, default to False
whether to sort the resulting labels

additional kwargs to control time-like groupers (when freq is passed)
additional kwargs to control time-like groupers (when ``freq`` is passed)

closed : closed end of interval; left or right
label : interval boundary to use for labeling; left or right
closed : closed end of interval; 'left' or 'right'
label : interval boundary to use for labeling; 'left' or 'right'
convention : {'start', 'end', 'e', 's'}
If grouper is PeriodIndex
base, loffset

Returns
-------
Expand All @@ -233,6 +234,7 @@ class Grouper(object):

>>> df.groupby(Grouper(level='date', freq='60s', axis=1))
"""
_attributes = ('key', 'level', 'freq', 'axis', 'sort')

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

def __repr__(self):
attrs_list = ["{}={!r}".format(attr_name, getattr(self, attr_name))
for attr_name in self._attributes
if getattr(self, attr_name) is not None]
attrs = ", ".join(attrs_list)
cls_name = self.__class__.__name__
return "{}({})".format(cls_name, attrs)


class GroupByPlot(PandasObject):
"""
Expand Down
19 changes: 7 additions & 12 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,22 +1014,18 @@ class TimeGrouper(Grouper):
Parameters
----------
freq : pandas date offset or offset alias for identifying bin edges
closed : closed end of interval; left or right
label : interval boundary to use for labeling; left or right
nperiods : optional, integer
closed : closed end of interval; 'left' or 'right'
label : interval boundary to use for labeling; 'left' or 'right'
convention : {'start', 'end', 'e', 's'}
If axis is PeriodIndex

Notes
-----
Use begin, end, nperiods to generate intervals that cannot be derived
directly from the associated object
"""
_attributes = Grouper._attributes + ('closed', 'label', 'how',
'loffset', 'kind', 'convention',
'base')

def __init__(self, freq='Min', closed=None, label=None, how='mean',
nperiods=None, axis=0,
fill_method=None, limit=None, loffset=None, kind=None,
convention=None, base=0, **kwargs):
axis=0, fill_method=None, limit=None, loffset=None,
kind=None, convention=None, base=0, **kwargs):
freq = to_offset(freq)

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

self.closed = closed
self.label = label
self.nperiods = nperiods
self.kind = kind

self.convention = convention or 'E'
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
from .common import MixIn


class TestGrouper(object):

def test_repr(self):
# GH18203
result = repr(pd.Grouper(key='A', level='B'))
expected = "Grouper(key='A', level='B', axis=0, sort=False)"
assert result == expected


class TestGroupBy(MixIn):

def test_basic(self):
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -3416,3 +3416,11 @@ def test_aggregate_with_nat(self):

# if NaT is included, 'var', 'std', 'mean', 'first','last'
# and 'nth' doesn't work yet

def test_repr(self):
# GH18203
result = repr(TimeGrouper(key='A', freq='H'))
expected = ("TimeGrouper(key='A', freq=<Hour>, axis=0, sort=True, "
"closed='left', label='left', how='mean', "
"convention='e', base=0)")
assert result == expected