Skip to content

repr string for pd.Grouper #17727

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

Closed
wants to merge 3 commits into from
Closed
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
15 changes: 14 additions & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pandas.compat import (
zip, range, lzip,
callable, map
callable, map, signature
)

from pandas import compat
Expand Down Expand Up @@ -234,6 +234,8 @@ 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:
from pandas.core.resample import TimeGrouper
Expand Down Expand Up @@ -333,6 +335,17 @@ def _set_grouper(self, obj, sort=False):
def groups(self):
return self.grouper.groups

def __repr__(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why you need to introspect at all. when repr is called all of the values are set, simply iterate thru them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is to get the difference between the default values and the actual value, and only use attrubutes that deviate from the default values.

grouper_defaults = compat.signature(self.__init__).defaults
sd = self.__dict__
attrs = collections.OrderedDict()
for k, v in zip(self._attributes, grouper_defaults):
if k in sd and sd[k] != v:
attrs[k] = sd[k]
attrs = ", ".join("{}={!r}".format(k, v) for k, v in attrs.items())
cls_name = self.__class__.__name__
return "{}({})".format(cls_name, attrs)


class GroupByPlot(PandasObject):
"""
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import timedelta
import collections
import numpy as np
import warnings
import copy
Expand Down Expand Up @@ -1026,13 +1027,18 @@ class TimeGrouper(Grouper):
directly from the associated object
"""

_attributes = ['key', 'level', 'freq', 'axis', 'sort', 'closed', 'label',
'how', 'nperiods', 'fill_method', 'limit',
'loffset', 'kind', 'convention', 'base']
_end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}

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):
freq = to_offset(freq)

end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'])
end_types = self._end_types
rule = freq.rule_code
if (rule in end_types or
('-' in rule and rule[:rule.find('-')] in end_types)):
Expand All @@ -1047,6 +1053,7 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
label = 'left'

self.closed = closed
self.freq = freq
self.label = label
self.nperiods = nperiods
self.kind = kind
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 @@ -3177,6 +3177,14 @@ def setup_method(self, method):
self.ts = Series(np.random.randn(1000),
index=date_range('1/1/2000', periods=1000))

def test_timegrouper_repr(self):
# Added in GH17727
result = repr(TimeGrouper(key='key', freq='50Min', label='right'))
expected = ("TimeGrouper(key='key', freq=<50 * Minutes>, axis=0,"
" sort=True, closed='left', label='right', how='mean', "
"loffset=None)")
assert result == expected

def test_apply(self):
with tm.assert_produces_warning(FutureWarning,
check_stacklevel=False):
Expand Down