diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 7ed92935a0991..2bfc57d7f5dcd 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -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 ^^^^^^ diff --git a/pandas/core/resample.py b/pandas/core/resample.py index ae59014ac34f4..2ada4d758d463 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -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 ) diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index 38801832829b0..b60fd10d745c1 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -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