Skip to content

Fix #20744 . loffset ignored in upsample #20867

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 1 commit 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.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ Groupby/Resample/Rolling
- Bug in :func:`DataFrameGroupBy.cumsum` and :func:`DataFrameGroupBy.cumprod` when ``skipna`` was passed (:issue:`19806`)
- Bug in :func:`DataFrame.resample` that dropped timezone information (:issue:`13238`)
- Bug in :func:`DataFrame.groupby` where transformations using ``np.all`` and ``np.any`` were raising a ``ValueError`` (:issue:`20653`)
- Bug in :func:`DatetimeIndexResampler._upsample` where ``loffset`` was being ignored (:issue:`20744`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a user visible note. users dont' know what _upsample is as its an internal method. pls rephrase.


Sparse
^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ def _upsample(self, method, limit=None, fill_value=None):
result = obj.reindex(res_index, method=method,
limit=limit, fill_value=fill_value)

result = self._apply_loffset(result)
return self._wrap_result(result)

def _wrap_result(self, result):
Expand Down
31 changes: 21 additions & 10 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,22 +1153,33 @@ def test_resample_loffset(self):
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min')
s = Series(np.random.randn(14), index=rng)

result = s.resample('5min', closed='right', label='right',
loffset=timedelta(minutes=1)).mean()
idx = date_range('1/1/2000', periods=4, freq='5min')
expected = Series([s[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()],
index=idx + timedelta(minutes=1))
assert_series_equal(result, expected)

expected = s.resample(
expected_downsample = Series([s[0], s[1:6].mean(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make a separate test instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use result & expected rather than changing things

s[6:11].mean(), s[11:].mean()],
index=idx + timedelta(minutes=1))
# GH 20744
expected_upsample = Series([s[0], s[5], s[10], s[-1]],
index=idx + timedelta(minutes=1))

# loffset should work for upsample and downsample
result_downsample = s.resample('5min', closed='right', label='right',
loffset=timedelta(minutes=1)).mean()
result_upsample = s.resample('5min', closed='right', label='right',
loffset=timedelta(minutes=1)).ffill()

assert_series_equal(result_downsample, expected_downsample)
assert_series_equal(result_upsample, expected_upsample)

# loffset should allow various types
result = s.resample(
'5min', closed='right', label='right',
loffset='1min').mean()
assert_series_equal(result, expected)
assert_series_equal(result, expected_downsample)

expected = s.resample(
result = s.resample(
'5min', closed='right', label='right',
loffset=Minute(1)).mean()
assert_series_equal(result, expected)
assert_series_equal(result, expected_downsample)

assert result.index.freq == Minute(5)

Expand Down