Skip to content

Commit

Permalink
BUG: rolling not accepting Timedelta-like window args (pandas-dev#15443)
Browse files Browse the repository at this point in the history
Remove unnecessary pd.Timedelta
  • Loading branch information
mroeschke authored and jorisvandenbossche committed Feb 18, 2017
1 parent a17a03a commit 29aeffb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ Bug Fixes
- Bug in ``.to_json()`` causing single byte ascii characters to be expanded to four byte unicode (:issue:`15344`)
- Bug in ``.read_json()`` for Python 2 where ``lines=True`` and contents contain non-ascii unicode characters (:issue:`15132`)
- Bug in ``.rolling/expanding()`` functions where ``count()`` was not counting ``np.Inf``, nor handling ``object`` dtypes (:issue:`12541`)
- Bug in ``.rolling()`` where ``pd.Timedelta`` or ``datetime.timedelta`` was not accepted as a ``window`` argument (:issue:`15440`)
- Bug in ``DataFrame.resample().median()`` if duplicate column names are present (:issue:`14233`)

- Bug in ``DataFrame.groupby().describe()`` when grouping on ``Index`` containing tuples (:issue:`14848`)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import warnings
import numpy as np
from collections import defaultdict
from datetime import timedelta

from pandas.types.generic import (ABCSeries,
ABCDataFrame,
Expand Down Expand Up @@ -1014,7 +1015,8 @@ def validate(self):

# we allow rolling on a datetimelike index
if (self.is_datetimelike and
isinstance(self.window, (compat.string_types, DateOffset))):
isinstance(self.window, (compat.string_types, DateOffset,
timedelta))):

self._validate_monotonic()
freq = self._validate_freq()
Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import warnings
from warnings import catch_warnings

from datetime import datetime
from datetime import datetime, timedelta
from numpy.random import randn
import numpy as np
from distutils.version import LooseVersion
Expand Down Expand Up @@ -401,6 +401,24 @@ def test_constructor_with_win_type(self):
with self.assertRaises(ValueError):
c(-1, win_type='boxcar')

def test_constructor_with_timedelta_window(self):
# GH 15440
n = 10
df = pd.DataFrame({'value': np.arange(n)},
index=pd.date_range('2015-12-24',
periods=n,
freq="D"))
expected_data = np.append([0., 1.], np.arange(3., 27., 3))
for window in [timedelta(days=3), pd.Timedelta(days=3)]:
result = df.rolling(window=window).sum()
expected = pd.DataFrame({'value': expected_data},
index=pd.date_range('2015-12-24',
periods=n,
freq="D"))
tm.assert_frame_equal(result, expected)
expected = df.rolling('3D').sum()
tm.assert_frame_equal(result, expected)

def test_numpy_compat(self):
# see gh-12811
r = rwindow.Rolling(Series([2, 4, 6]), window=2)
Expand Down

0 comments on commit 29aeffb

Please sign in to comment.