diff --git a/doc/source/whatsnew/v0.18.1.txt b/doc/source/whatsnew/v0.18.1.txt index 1179a347e4c46..6f3a5cc45c918 100644 --- a/doc/source/whatsnew/v0.18.1.txt +++ b/doc/source/whatsnew/v0.18.1.txt @@ -169,3 +169,4 @@ Bug Fixes - Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`) - Bug in ``Series.name`` when ``name`` attribute can be a hashable type (:issue:`12610`) - Bug in ``.describe()`` resets categorical columns information (:issue:`11558`) +- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`) diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index 0ac10eb4fa15b..8d922dd548e50 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -372,8 +372,26 @@ def _groupby_and_aggregate(self, grouper, how, *args, **kwargs): # try to evaluate result = grouped.apply(how, *args, **kwargs) + result = self._apply_loffset(result) + return self._wrap_result(result) + def _apply_loffset(self, result): + """if loffset if set, offset the result index""" + loffset = self.loffset + if isinstance(loffset, compat.string_types): + loffset = to_offset(self.loffset) + + needs_offset = ( + isinstance(loffset, (DateOffset, timedelta)) and + isinstance(result.index, DatetimeIndex) and + len(result.index) > 0 + ) + if needs_offset: + result.index = result.index + loffset + + return result + def _wrap_result(self, result): """ potentially wrap any results """ return result @@ -572,14 +590,7 @@ def _downsample(self, how, **kwargs): result = obj.groupby( self.grouper, axis=self.axis).aggregate(how, **kwargs) - loffset = self.loffset - if isinstance(loffset, compat.string_types): - loffset = to_offset(self.loffset) - - if isinstance(loffset, (DateOffset, timedelta)) and \ - isinstance(result.index, DatetimeIndex) and \ - len(result.index) > 0: - result.index = result.index + loffset + result = self._apply_loffset(result) return self._wrap_result(result) diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index e2de3c5e01ba2..f4e2f056c0781 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -937,6 +937,28 @@ def test_resample_loffset(self): expected = ser.resample('w-sun', loffset=-bday).last() self.assertEqual(result.index[0] - bday, expected.index[0]) + def test_resample_loffset_count(self): + # GH 12725 + start_time = '1/1/2000 00:00:00' + rng = date_range(start_time, periods=100, freq='S') + ts = Series(np.random.randn(len(rng)), index=rng) + + result = ts.resample('10S', loffset='1s').count() + + expected_index = ( + date_range(start_time, periods=10, freq='10S') + + timedelta(seconds=1) + ) + expected = pd.Series(10, index=expected_index) + + assert_series_equal(result, expected) + + # Same issue should apply to .size() since it goes through + # same code path + result = ts.resample('10S', loffset='1s').size() + + assert_series_equal(result, expected) + def test_resample_upsample(self): # from daily dti = DatetimeIndex(start=datetime(2005, 1, 1),