Skip to content

Commit 3662413

Browse files
committed
CLN: rework apply_loffset a bit to make it applied only once
xref #13218
1 parent b2cdc02 commit 3662413

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

pandas/tseries/resample.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,11 @@ def aggregate(self, arg, *args, **kwargs):
319319
self._set_binner()
320320
result, how = self._aggregate(arg, *args, **kwargs)
321321
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)
330325

326+
result = self._apply_loffset(result)
331327
return result
332328

333329
agg = aggregate
@@ -410,31 +406,31 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
410406
result = grouped.apply(how, *args, **kwargs)
411407

412408
result = self._apply_loffset(result)
413-
414409
return self._wrap_result(result)
415410

416411
def _apply_loffset(self, result):
417412
"""
418413
if loffset is set, offset the result index
419414
415+
This is NOT an idempotent routine, it will be applied
416+
exactly once to the result.
417+
420418
Parameters
421419
----------
422420
result : Series or DataFrame
423421
the result of resample
424422
"""
425-
loffset = self.loffset
426-
if isinstance(loffset, compat.string_types):
427-
loffset = to_offset(self.loffset)
428423

429424
needs_offset = (
430-
isinstance(loffset, (DateOffset, timedelta)) and
425+
isinstance(self.loffset, (DateOffset, timedelta)) and
431426
isinstance(result.index, DatetimeIndex) and
432427
len(result.index) > 0
433428
)
434429

435430
if needs_offset:
436-
result.index = result.index + loffset
431+
result.index = result.index + self.loffset
437432

433+
self.loffset = None
438434
return result
439435

440436
def _get_resampler_for_grouping(self, groupby, **kwargs):
@@ -445,6 +441,7 @@ def _wrap_result(self, result):
445441
""" potentially wrap any results """
446442
if isinstance(result, com.ABCSeries) and self._selection is not None:
447443
result.name = self._selection
444+
448445
return result
449446

450447
def pad(self, limit=None):
@@ -706,7 +703,6 @@ def _downsample(self, how, **kwargs):
706703
self.grouper, axis=self.axis).aggregate(how, **kwargs)
707704

708705
result = self._apply_loffset(result)
709-
710706
return self._wrap_result(result)
711707

712708
def _adjust_binner_for_upsample(self, binner):
@@ -810,11 +806,7 @@ def aggregate(self, arg, *args, **kwargs):
810806
if result is None:
811807
result = self._downsample(arg, *args, **kwargs)
812808

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)
818810
return result
819811

820812
agg = aggregate
@@ -1022,7 +1014,10 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
10221014
self.convention = convention or 'E'
10231015
self.convention = self.convention.lower()
10241016

1017+
if isinstance(loffset, compat.string_types):
1018+
loffset = to_offset(loffset)
10251019
self.loffset = loffset
1020+
10261021
self.how = how
10271022
self.fill_method = fill_method
10281023
self.limit = limit

pandas/tseries/tests/test_resample.py

+7
Original file line numberDiff line numberDiff line change
@@ -777,19 +777,26 @@ def test_resample_loffset_arg_type(self):
777777
expected_index = self.create_index(df.index[0],
778778
periods=len(df.index) / 2,
779779
freq='2D')
780+
780781
# loffset coreces PeriodIndex to DateTimeIndex
781782
if isinstance(expected_index, PeriodIndex):
782783
expected_index = expected_index.to_timestamp()
784+
783785
expected_index += timedelta(hours=2)
784786
expected = DataFrame({'value': expected_means}, index=expected_index)
787+
785788
for arg in ['mean', {'value': 'mean'}, ['mean']]:
789+
786790
result_agg = df.resample('2D', loffset='2H').agg(arg)
791+
787792
with tm.assert_produces_warning(FutureWarning,
788793
check_stacklevel=False):
789794
result_how = df.resample('2D', how=arg, loffset='2H')
795+
790796
if isinstance(arg, list):
791797
expected.columns = pd.MultiIndex.from_tuples([('value',
792798
'mean')])
799+
793800
# GH 13022, 7687 - TODO: fix resample w/ TimedeltaIndex
794801
if isinstance(expected.index, TimedeltaIndex):
795802
with tm.assertRaises(AssertionError):

0 commit comments

Comments
 (0)