Skip to content

Commit 967512d

Browse files
author
tp
committed
Changed _attributes to use signature + changed tests to have ordered arguments for TimeGrouper
1 parent 236b12d commit 967512d

File tree

4 files changed

+20
-25
lines changed

4 files changed

+20
-25
lines changed

pandas/core/groupby.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from pandas.compat import (
1111
zip, range, lzip,
12-
callable, map
12+
callable, map, signature
1313
)
1414

1515
from pandas import compat
@@ -233,10 +233,6 @@ class Grouper(object):
233233
234234
>>> df.groupby(Grouper(level='date', freq='60s', axis=1))
235235
"""
236-
_attributes = collections.OrderedDict((('key', None), ('level', None),
237-
('freq', None), ('axis', 0),
238-
('sort', False)
239-
))
240236

241237
def __new__(cls, *args, **kwargs):
242238
if kwargs.get('freq') is not None:
@@ -256,6 +252,11 @@ def __init__(self, key=None, level=None, freq=None, axis=0, sort=False):
256252
self.indexer = None
257253
self.binner = None
258254

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+
259260
@property
260261
def ax(self):
261262
return self.grouper
@@ -338,9 +339,9 @@ def groups(self):
338339
return self.grouper.groups
339340

340341
def __repr__(self):
341-
defaults = self._attributes
342342
sd = self.__dict__
343-
attrs = collections.OrderedDict((k, sd[k]) for k, v in defaults.items()
343+
attr = self._attributes
344+
attrs = collections.OrderedDict((k, sd[k]) for k, v in attr.items()
344345
if k in sd and sd[k] != v)
345346
attrs = ", ".join("{}={!r}".format(k, v) for k, v in attrs.items())
346347
cls_name = self.__class__.__name__

pandas/core/resample.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,16 +1022,6 @@ class TimeGrouper(Grouper):
10221022
Use begin, end, nperiods to generate intervals that cannot be derived
10231023
directly from the associated object
10241024
"""
1025-
# _attributes is used in __repr__below
1026-
_attributes = Grouper._attributes.copy()
1027-
_attributes.update((('freq', 'Min'), ('closed', None), ('label', None),
1028-
('how', 'mean'), ('nperiods', None), ('axis', 0),
1029-
('fill_method', None), ('limit', None),
1030-
('loffset', None), ('kind', None),
1031-
('convention', None), ('base', 0),
1032-
('convention', 'e'), ('sort', True),
1033-
))
1034-
_end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}
10351025

10361026
def __init__(self, freq='Min', closed=None, label=None, how='mean',
10371027
nperiods=None, axis=0,
@@ -1076,6 +1066,14 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
10761066

10771067
super(TimeGrouper, self).__init__(freq=freq, axis=axis, **kwargs)
10781068

1069+
# _attributes is used in __repr__below
1070+
_attributes = Grouper._attributes.copy()
1071+
_attributes.update(
1072+
(k, v) for k, v in zip(compat.signature(__init__).args[1:],
1073+
compat.signature(__init__).defaults))
1074+
_attributes.update(sort=True, convention='e')
1075+
_end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}
1076+
10791077
def _get_resampler(self, obj, kind=None):
10801078
"""
10811079
return my resampler or raise if we have an invalid axis

pandas/tests/groupby/test_groupby.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,8 @@ def test_grouper_repr(self):
136136
assert result == "Grouper(key='A')"
137137

138138
result = repr(pd.Grouper(key='key', freq='50Min', label='right'))
139-
cls_name_result, attrib_result = result.split('(')
140-
attrib_result = set(attrib_result.rstrip(')').split(', '))
141-
assert cls_name_result == 'TimeGrouper'
142-
assert attrib_result == {"key='key'", "freq='50T'", "label='right'"}
139+
expected = "TimeGrouper(key='key', freq='50T', label='right')"
140+
assert result == expected
143141

144142
def test_grouper_multilevel_freq(self):
145143

pandas/tests/test_resample.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3160,10 +3160,8 @@ def setup_method(self, method):
31603160
def test_timegrouper_repr(self):
31613161
# Added in GH17727
31623162
result = repr(TimeGrouper(key='key', freq='50Min', label='right'))
3163-
cls_name_result, attrib_result = result.split('(')
3164-
attrib_result = set(attrib_result.rstrip(')').split(', '))
3165-
assert cls_name_result == 'TimeGrouper'
3166-
assert attrib_result == {"key='key'", "freq='50T'", "label='right'"}
3163+
expected = "TimeGrouper(key='key', freq='50T', label='right')"
3164+
assert result == expected
31673165

31683166
def test_apply(self):
31693167
with tm.assert_produces_warning(FutureWarning,

0 commit comments

Comments
 (0)