Skip to content

Commit 7c7bb7a

Browse files
discortaeltanawy
authored andcommitted
Fixed loffset with numpy timedelta (pandas-dev#22482)
1 parent 4cb55a4 commit 7c7bb7a

File tree

3 files changed

+9
-14
lines changed

3 files changed

+9
-14
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ Groupby/Resample/Rolling
707707
- Multiple bugs in :func:`pandas.core.Rolling.min` with ``closed='left'` and a
708708
datetime-like index leading to incorrect results and also segfault. (:issue:`21704`)
709709
- Bug in :meth:`Resampler.apply` when passing postiional arguments to applied func (:issue:`14615`).
710+
- Bug in :meth:`Series.resample` when passing ``numpy.timedelta64`` to `loffset` kwarg (:issue:`7687`).
710711

711712
Sparse
712713
^^^^^^

pandas/core/resample.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ def _apply_loffset(self, result):
366366
"""
367367

368368
needs_offset = (
369-
isinstance(self.loffset, (DateOffset, timedelta)) and
369+
isinstance(self.loffset, (DateOffset, timedelta,
370+
np.timedelta64)) and
370371
isinstance(result.index, DatetimeIndex) and
371372
len(result.index) > 0
372373
)

pandas/tests/test_resample.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -1173,27 +1173,20 @@ def test_resample_frame_basic(self):
11731173
df.resample('M', kind='period').mean()
11741174
df.resample('W-WED', kind='period').mean()
11751175

1176-
def test_resample_loffset(self):
1176+
@pytest.mark.parametrize('loffset', [timedelta(minutes=1),
1177+
'1min', Minute(1),
1178+
np.timedelta64(1, 'm')])
1179+
def test_resample_loffset(self, loffset):
1180+
# GH 7687
11771181
rng = date_range('1/1/2000 00:00:00', '1/1/2000 00:13:00', freq='min')
11781182
s = Series(np.random.randn(14), index=rng)
11791183

11801184
result = s.resample('5min', closed='right', label='right',
1181-
loffset=timedelta(minutes=1)).mean()
1185+
loffset=loffset).mean()
11821186
idx = date_range('1/1/2000', periods=4, freq='5min')
11831187
expected = Series([s[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()],
11841188
index=idx + timedelta(minutes=1))
11851189
assert_series_equal(result, expected)
1186-
1187-
expected = s.resample(
1188-
'5min', closed='right', label='right',
1189-
loffset='1min').mean()
1190-
assert_series_equal(result, expected)
1191-
1192-
expected = s.resample(
1193-
'5min', closed='right', label='right',
1194-
loffset=Minute(1)).mean()
1195-
assert_series_equal(result, expected)
1196-
11971190
assert result.index.freq == Minute(5)
11981191

11991192
# from daily

0 commit comments

Comments
 (0)