Skip to content

Commit 510ca45

Browse files
committed
BUG: rolling not accepting Timedelta-like window args
Remove unnecessary pd.Timedelta
1 parent f65a641 commit 510ca45

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ Bug Fixes
551551
- Bug in ``.to_json()`` causing single byte ascii characters to be expanded to four byte unicode (:issue:`15344`)
552552
- Bug in ``.read_json()`` for Python 2 where ``lines=True`` and contents contain non-ascii unicode characters (:issue:`15132`)
553553
- Bug in ``.rolling/expanding()`` functions where ``count()`` was not counting ``np.Inf``, nor handling ``object`` dtypes (:issue:`12541`)
554+
- Bug in ``.rolling()`` where ``pd.Timedelta`` or ``datetime.timedelta`` was not accepted as a ``window`` argument (:issue:`15440`)
554555
- Bug in ``DataFrame.resample().median()`` if duplicate column names are present (:issue:`14233`)
555556

556557
- Bug in ``DataFrame.groupby().describe()`` when grouping on ``Index`` containing tuples (:issue:`14848`)

pandas/core/window.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import warnings
1111
import numpy as np
1212
from collections import defaultdict
13+
from datetime import timedelta
1314

1415
from pandas.types.generic import (ABCSeries,
1516
ABCDataFrame,
@@ -1014,7 +1015,8 @@ def validate(self):
10141015

10151016
# we allow rolling on a datetimelike index
10161017
if (self.is_datetimelike and
1017-
isinstance(self.window, (compat.string_types, DateOffset))):
1018+
isinstance(self.window, (compat.string_types, DateOffset,
1019+
timedelta))):
10181020

10191021
self._validate_monotonic()
10201022
freq = self._validate_freq()

pandas/tests/test_window.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import warnings
55
from warnings import catch_warnings
66

7-
from datetime import datetime
7+
from datetime import datetime, timedelta
88
from numpy.random import randn
99
import numpy as np
1010
from distutils.version import LooseVersion
@@ -401,6 +401,24 @@ def test_constructor_with_win_type(self):
401401
with self.assertRaises(ValueError):
402402
c(-1, win_type='boxcar')
403403

404+
def test_constructor_with_timedelta_window(self):
405+
# GH 15440
406+
n = 10
407+
df = pd.DataFrame({'value': np.arange(n)},
408+
index=pd.date_range('2015-12-24',
409+
periods=n,
410+
freq="D"))
411+
expected_data = np.append([0., 1.], np.arange(3., 27., 3))
412+
for window in [timedelta(days=3), pd.Timedelta(days=3)]:
413+
result = df.rolling(window=window).sum()
414+
expected = pd.DataFrame({'value': expected_data},
415+
index=pd.date_range('2015-12-24',
416+
periods=n,
417+
freq="D"))
418+
tm.assert_frame_equal(result, expected)
419+
expected = df.rolling('3D').sum()
420+
tm.assert_frame_equal(result, expected)
421+
404422
def test_numpy_compat(self):
405423
# see gh-12811
406424
r = rwindow.Rolling(Series([2, 4, 6]), window=2)

0 commit comments

Comments
 (0)