@@ -319,15 +319,11 @@ def aggregate(self, arg, *args, **kwargs):
319
319
self ._set_binner ()
320
320
result , how = self ._aggregate (arg , * args , ** kwargs )
321
321
if result is None :
322
- return self ._groupby_and_aggregate (arg ,
323
- * args ,
324
- ** kwargs )
325
-
326
- # if arg was a string, _aggregate called resampler's _downsample or
327
- # _groupby_and_agg methods, which would've already applied the loffset
328
- if not isinstance (arg , compat .string_types ):
329
- result = self ._apply_loffset (result )
322
+ result = self ._groupby_and_aggregate (arg ,
323
+ * args ,
324
+ ** kwargs )
330
325
326
+ result = self ._apply_loffset (result )
331
327
return result
332
328
333
329
agg = aggregate
@@ -410,31 +406,31 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
410
406
result = grouped .apply (how , * args , ** kwargs )
411
407
412
408
result = self ._apply_loffset (result )
413
-
414
409
return self ._wrap_result (result )
415
410
416
411
def _apply_loffset (self , result ):
417
412
"""
418
413
if loffset is set, offset the result index
419
414
415
+ This is NOT an idempotent routine, it will be applied
416
+ exactly once to the result.
417
+
420
418
Parameters
421
419
----------
422
420
result : Series or DataFrame
423
421
the result of resample
424
422
"""
425
- loffset = self .loffset
426
- if isinstance (loffset , compat .string_types ):
427
- loffset = to_offset (self .loffset )
428
423
429
424
needs_offset = (
430
- isinstance (loffset , (DateOffset , timedelta )) and
425
+ isinstance (self . loffset , (DateOffset , timedelta )) and
431
426
isinstance (result .index , DatetimeIndex ) and
432
427
len (result .index ) > 0
433
428
)
434
429
435
430
if needs_offset :
436
- result .index = result .index + loffset
431
+ result .index = result .index + self . loffset
437
432
433
+ self .loffset = None
438
434
return result
439
435
440
436
def _get_resampler_for_grouping (self , groupby , ** kwargs ):
@@ -445,6 +441,7 @@ def _wrap_result(self, result):
445
441
""" potentially wrap any results """
446
442
if isinstance (result , com .ABCSeries ) and self ._selection is not None :
447
443
result .name = self ._selection
444
+
448
445
return result
449
446
450
447
def pad (self , limit = None ):
@@ -706,7 +703,6 @@ def _downsample(self, how, **kwargs):
706
703
self .grouper , axis = self .axis ).aggregate (how , ** kwargs )
707
704
708
705
result = self ._apply_loffset (result )
709
-
710
706
return self ._wrap_result (result )
711
707
712
708
def _adjust_binner_for_upsample (self , binner ):
@@ -810,11 +806,7 @@ def aggregate(self, arg, *args, **kwargs):
810
806
if result is None :
811
807
result = self ._downsample (arg , * args , ** kwargs )
812
808
813
- # if arg was a string, _aggregate called resamplers' _downsample or
814
- # _groupby_and_agg methods, which would've already applied the loffset
815
- if not isinstance (arg , compat .string_types ):
816
- result = self ._apply_loffset (result )
817
-
809
+ result = self ._apply_loffset (result )
818
810
return result
819
811
820
812
agg = aggregate
@@ -1022,7 +1014,10 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
1022
1014
self .convention = convention or 'E'
1023
1015
self .convention = self .convention .lower ()
1024
1016
1017
+ if isinstance (loffset , compat .string_types ):
1018
+ loffset = to_offset (loffset )
1025
1019
self .loffset = loffset
1020
+
1026
1021
self .how = how
1027
1022
self .fill_method = fill_method
1028
1023
self .limit = limit
0 commit comments