Skip to content

BUG: rolling not accepting Timedelta-like window args (#15440) #15443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need pd.Timedelta here (its a sub-class of timedelta).

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