Skip to content

BUG: loffset has no effect when passing in a numpy.timedelta64 #22482

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

Merged
merged 1 commit into from
Sep 4, 2018
Merged
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.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ Groupby/Resample/Rolling
- Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'` and a
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
- Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`).
- Bug in :meth:`Series.resample` when passing ``numpy.timedelta64`` to `loffset` kwarg (:issue:`7687`).

Sparse
^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@ def _apply_loffset(self, result):
"""

needs_offset = (
isinstance(self.loffset, (DateOffset, timedelta)) and
isinstance(self.loffset, (DateOffset, timedelta,
np.timedelta64)) and
isinstance(result.index, DatetimeIndex) and
len(result.index) > 0
)
Expand Down
19 changes: 6 additions & 13 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,27 +1173,20 @@ def test_resample_frame_basic(self):
df.resample('M', kind='period').mean()
df.resample('W-WED', kind='period').mean()

def test_resample_loffset(self):
@pytest.mark.parametrize('loffset', [timedelta(minutes=1),
Copy link
Contributor

Choose a reason for hiding this comment

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

if you want to use hypothesis would be great here :> (can be followup, not sure how easy)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback

There are no many examples in pandas codebase how to use hypothesis. Furthermore, I know only 4 cases which need to test: timedelta(minutes=1), '1min', Minute(1), np.timedelta64(1, 'm'). Did I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ping @jreback

'1min', Minute(1),
np.timedelta64(1, 'm')])
def test_resample_loffset(self, loffset):
Copy link
Member

Choose a reason for hiding this comment

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

Can we put a comment referencing the issue next to the relevant input value to loffset?

# GH 7687
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()
loffset=loffset).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(
'5min', closed='right', label='right',
loffset='1min').mean()
assert_series_equal(result, expected)

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

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

# from daily
Expand Down