Skip to content

Commit b6959c8

Browse files
onesandzeroesjreback
authored andcommitted
BUG: loffset argument not applied for resample().count() on timeseries
closes #12725 The code to do this already existed in the `_downsample` method, which is called when using functions like `mean`. `max`, etc., but there was nothing in the `_groupby_and_aggregate` method used for `count` and `size`. If we pull out the offset code from the `_downsample` method into a separate method, we can reuse it without duplicating it. Author: onesandzeroes <[email protected]> Closes #12757 from onesandzeroes/loffset-count and squashes the following commits: 627429a [onesandzeroes] Add to release notes fa021bf [onesandzeroes] pep8 style fixes 0c0ca34 [onesandzeroes] Update comment re: issue number dc79369 [onesandzeroes] Apply loffset when aggregating, e.g. using count() 15aadb9 [onesandzeroes] Extract loffset handling into a general method 70e9765 [onesandzeroes] Add test case for loffset when using count() method
1 parent b04ee05 commit b6959c8

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ Bug Fixes
169169
- Bug in ``pivot_table`` when ``margins=True`` and ``dropna=True`` where nulls still contributed to margin count (:issue:`12577`)
170170
- Bug in ``Series.name`` when ``name`` attribute can be a hashable type (:issue:`12610`)
171171
- Bug in ``.describe()`` resets categorical columns information (:issue:`11558`)
172+
- Bug where ``loffset`` argument was not applied when calling ``resample().count()`` on a timeseries (:issue:`12725`)

pandas/tseries/resample.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,26 @@ def _groupby_and_aggregate(self, grouper, how, *args, **kwargs):
372372
# try to evaluate
373373
result = grouped.apply(how, *args, **kwargs)
374374

375+
result = self._apply_loffset(result)
376+
375377
return self._wrap_result(result)
376378

379+
def _apply_loffset(self, result):
380+
"""if loffset if set, offset the result index"""
381+
loffset = self.loffset
382+
if isinstance(loffset, compat.string_types):
383+
loffset = to_offset(self.loffset)
384+
385+
needs_offset = (
386+
isinstance(loffset, (DateOffset, timedelta)) and
387+
isinstance(result.index, DatetimeIndex) and
388+
len(result.index) > 0
389+
)
390+
if needs_offset:
391+
result.index = result.index + loffset
392+
393+
return result
394+
377395
def _wrap_result(self, result):
378396
""" potentially wrap any results """
379397
return result
@@ -572,14 +590,7 @@ def _downsample(self, how, **kwargs):
572590
result = obj.groupby(
573591
self.grouper, axis=self.axis).aggregate(how, **kwargs)
574592

575-
loffset = self.loffset
576-
if isinstance(loffset, compat.string_types):
577-
loffset = to_offset(self.loffset)
578-
579-
if isinstance(loffset, (DateOffset, timedelta)) and \
580-
isinstance(result.index, DatetimeIndex) and \
581-
len(result.index) > 0:
582-
result.index = result.index + loffset
593+
result = self._apply_loffset(result)
583594

584595
return self._wrap_result(result)
585596

pandas/tseries/tests/test_resample.py

+22
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,28 @@ def test_resample_loffset(self):
937937
expected = ser.resample('w-sun', loffset=-bday).last()
938938
self.assertEqual(result.index[0] - bday, expected.index[0])
939939

940+
def test_resample_loffset_count(self):
941+
# GH 12725
942+
start_time = '1/1/2000 00:00:00'
943+
rng = date_range(start_time, periods=100, freq='S')
944+
ts = Series(np.random.randn(len(rng)), index=rng)
945+
946+
result = ts.resample('10S', loffset='1s').count()
947+
948+
expected_index = (
949+
date_range(start_time, periods=10, freq='10S') +
950+
timedelta(seconds=1)
951+
)
952+
expected = pd.Series(10, index=expected_index)
953+
954+
assert_series_equal(result, expected)
955+
956+
# Same issue should apply to .size() since it goes through
957+
# same code path
958+
result = ts.resample('10S', loffset='1s').size()
959+
960+
assert_series_equal(result, expected)
961+
940962
def test_resample_upsample(self):
941963
# from daily
942964
dti = DatetimeIndex(start=datetime(2005, 1, 1),

0 commit comments

Comments
 (0)