Skip to content

Commit

Permalink
Fixed loffset with numpy timedelta
Browse files Browse the repository at this point in the history
  • Loading branch information
discort committed Aug 29, 2018
1 parent d5f30fa commit 3e2ef06
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
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 @@ -703,6 +703,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),
'1min', Minute(1),
np.timedelta64(1, 'm')])
def test_resample_loffset(self, 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

0 comments on commit 3e2ef06

Please sign in to comment.