Skip to content

Commit 5595156

Browse files
author
tp
committed
Changed _attributes to use signature + changed tests to have ordered arguments for TimeGrouper
1 parent 63bbbde commit 5595156

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
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
@@ -1026,16 +1026,6 @@ class TimeGrouper(Grouper):
10261026
Use begin, end, nperiods to generate intervals that cannot be derived
10271027
directly from the associated object
10281028
"""
1029-
# _attributes is used in __repr__below
1030-
_attributes = Grouper._attributes.copy()
1031-
_attributes.update((('freq', 'Min'), ('closed', None), ('label', None),
1032-
('how', 'mean'), ('nperiods', None), ('axis', 0),
1033-
('fill_method', None), ('limit', None),
1034-
('loffset', None), ('kind', None),
1035-
('convention', None), ('base', 0),
1036-
('convention', 'e'), ('sort', True),
1037-
))
1038-
_end_types = {'M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'}
10391029

10401030
def __init__(self, freq='Min', closed=None, label=None, how='mean',
10411031
nperiods=None, axis=0,
@@ -1080,6 +1070,14 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
10801070

10811071
super(TimeGrouper, self).__init__(freq=freq, axis=axis, **kwargs)
10821072

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+
10831081
def _get_resampler(self, obj, kind=None):
10841082
"""
10851083
return my resampler or raise if we have an invalid axis

pandas/tests/test_resample.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,10 +3180,8 @@ 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-
cls_name_result, attrib_result = result.split('(')
3184-
attrib_result = set(attrib_result.rstrip(')').split(', '))
3185-
assert cls_name_result == 'TimeGrouper'
3186-
assert attrib_result == {"key='key'", "freq='50T'", "label='right'"}
3183+
expected = "TimeGrouper(key='key', freq='50T', label='right')"
3184+
assert result == expected
31873185

31883186
def test_apply(self):
31893187
with tm.assert_produces_warning(FutureWarning,

0 commit comments

Comments
 (0)