Skip to content

BUG: loffset argument not applied for resample().count() on timeseries #12757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
27 changes: 19 additions & 8 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down