Skip to content

Commit 62f2de8

Browse files
author
tp
committed
New attempt at Grouper.__repr__
1 parent 5595156 commit 62f2de8

File tree

3 files changed

+15
-36
lines changed

3 files changed

+15
-36
lines changed

pandas/core/groupby.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ class Grouper(object):
234234
>>> df.groupby(Grouper(level='date', freq='60s', axis=1))
235235
"""
236236

237+
_attributes = ['key', 'level', 'freq', 'axis', 'sort']
238+
237239
def __new__(cls, *args, **kwargs):
238240
if kwargs.get('freq') is not None:
239241
from pandas.core.resample import TimeGrouper
@@ -252,11 +254,6 @@ def __init__(self, key=None, level=None, freq=None, axis=0, sort=False):
252254
self.indexer = None
253255
self.binner = None
254256

255-
# _attributes is used in __repr__below
256-
_attributes = collections.OrderedDict(
257-
(k, v) for k, v in zip(signature(__init__).args[1:],
258-
signature(__init__).defaults))
259-
260257
@property
261258
def ax(self):
262259
return self.grouper
@@ -339,10 +336,12 @@ def groups(self):
339336
return self.grouper.groups
340337

341338
def __repr__(self):
339+
grouper_defaults = compat.signature(self.__init__).defaults
342340
sd = self.__dict__
343-
attr = self._attributes
344-
attrs = collections.OrderedDict((k, sd[k]) for k, v in attr.items()
345-
if k in sd and sd[k] != v)
341+
attrs = collections.OrderedDict()
342+
for k, v in zip(self._attributes, grouper_defaults):
343+
if k in sd and sd[k] != v:
344+
attrs[k] = sd[k]
346345
attrs = ", ".join("{}={!r}".format(k, v) for k, v in attrs.items())
347346
cls_name = self.__class__.__name__
348347
return "{}({})".format(cls_name, attrs)

pandas/core/resample.py

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,11 @@ class TimeGrouper(Grouper):
10271027
directly from the associated object
10281028
"""
10291029

1030+
_attributes = ['key', 'level', 'freq', 'axis', 'sort', 'closed', 'label',
1031+
'how', 'nperiods', 'fill_method', 'limit',
1032+
'loffset', 'kind', 'convention', 'base']
1033+
_end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}
1034+
10301035
def __init__(self, freq='Min', closed=None, label=None, how='mean',
10311036
nperiods=None, axis=0,
10321037
fill_method=None, limit=None, loffset=None, kind=None,
@@ -1070,14 +1075,6 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
10701075

10711076
super(TimeGrouper, self).__init__(freq=freq, axis=axis, **kwargs)
10721077

1073-
# _attributes is used in __repr__below
1074-
_attributes = Grouper._attributes.copy()
1075-
_attributes.update(
1076-
(k, v) for k, v in zip(compat.signature(__init__).args[1:],
1077-
compat.signature(__init__).defaults))
1078-
_attributes.update(sort=True, convention='e')
1079-
_end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}
1080-
10811078
def _get_resampler(self, obj, kind=None):
10821079
"""
10831080
return my resampler or raise if we have an invalid axis
@@ -1300,25 +1297,6 @@ def _get_period_bins(self, ax):
13001297

13011298
return binner, bins, labels
13021299

1303-
def __repr__(self):
1304-
defaults = self._attributes.copy()
1305-
end_types = self._end_types
1306-
rule = self.freq.rule_code
1307-
if (rule in end_types or
1308-
('-' in rule and rule[:rule.find('-')] in end_types)):
1309-
defaults.update(closed='right', label='right')
1310-
else:
1311-
defaults.update(closed='left', label='left')
1312-
1313-
sd = self.__dict__
1314-
attrs = collections.OrderedDict((k, sd[k]) for k, v in defaults.items()
1315-
if k in sd and sd[k] != v)
1316-
if 'freq' in attrs:
1317-
attrs['freq'] = attrs['freq'].freqstr
1318-
attrs = ", ".join("{}={!r}".format(k, v) for k, v in attrs.items())
1319-
cls_name = self.__class__.__name__
1320-
return "{}({})".format(cls_name, attrs)
1321-
13221300

13231301
def _take_new_index(obj, indexer, new_index, axis=0):
13241302
from pandas.core.api import Series, DataFrame

pandas/tests/test_resample.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3180,7 +3180,9 @@ def setup_method(self, method):
31803180
def test_timegrouper_repr(self):
31813181
# Added in GH17727
31823182
result = repr(TimeGrouper(key='key', freq='50Min', label='right'))
3183-
expected = "TimeGrouper(key='key', freq='50T', label='right')"
3183+
expected = ("TimeGrouper(key='key', freq=<50 * Minutes>, axis=0,"
3184+
" sort=True, closed='left', label='right', how='mean', "
3185+
"loffset=None)")
31843186
assert result == expected
31853187

31863188
def test_apply(self):

0 commit comments

Comments
 (0)