1
1
from datetime import timedelta
2
+ import collections
2
3
import numpy as np
3
4
import warnings
4
5
import copy
@@ -1025,14 +1026,24 @@ class TimeGrouper(Grouper):
1025
1026
Use begin, end, nperiods to generate intervals that cannot be derived
1026
1027
directly from the associated object
1027
1028
"""
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' }
1028
1039
1029
1040
def __init__ (self , freq = 'Min' , closed = None , label = None , how = 'mean' ,
1030
1041
nperiods = None , axis = 0 ,
1031
1042
fill_method = None , limit = None , loffset = None , kind = None ,
1032
1043
convention = None , base = 0 , ** kwargs ):
1033
1044
freq = to_offset (freq )
1034
1045
1035
- end_types = set ([ 'M' , 'A' , 'Q' , 'BM' , 'BA' , 'BQ' , 'W' ])
1046
+ end_types = self . _end_types
1036
1047
rule = freq .rule_code
1037
1048
if (rule in end_types or
1038
1049
('-' in rule and rule [:rule .find ('-' )] in end_types )):
@@ -1047,6 +1058,7 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
1047
1058
label = 'left'
1048
1059
1049
1060
self .closed = closed
1061
+ self .freq = freq
1050
1062
self .label = label
1051
1063
self .nperiods = nperiods
1052
1064
self .kind = kind
@@ -1290,6 +1302,25 @@ def _get_period_bins(self, ax):
1290
1302
1291
1303
return binner , bins , labels
1292
1304
1305
+ def __repr__ (self ):
1306
+ defaults = self ._attributes .copy ()
1307
+ end_types = self ._end_types
1308
+ rule = self .freq .rule_code
1309
+ if (rule in end_types or
1310
+ ('-' in rule and rule [:rule .find ('-' )] in end_types )):
1311
+ defaults .update (closed = 'right' , label = 'right' )
1312
+ else :
1313
+ defaults .update (closed = 'left' , label = 'left' )
1314
+
1315
+ sd = self .__dict__
1316
+ attrs = collections .OrderedDict ((k , sd [k ]) for k , v in defaults .items ()
1317
+ if k in sd and sd [k ] != v )
1318
+ if 'freq' in attrs :
1319
+ attrs ['freq' ] = attrs ['freq' ].freqstr
1320
+ attrs = ", " .join ("{}={!r}" .format (k , v ) for k , v in attrs .items ())
1321
+ cls_name = self .__class__ .__name__
1322
+ return "{}({})" .format (cls_name , attrs )
1323
+
1293
1324
1294
1325
def _take_new_index (obj , indexer , new_index , axis = 0 ):
1295
1326
from pandas .core .api import Series , DataFrame
0 commit comments