Repository: pandas-dev/pandas

Issue:
Adding Timedelta parameter to time-based .rolling()
Starting with a simple DataFrame:

```python
n = 10
df = pd.DataFrame({'value': np.arange(n)},
                  index=pd.date_range('2015-12-24', periods=n, freq="D"))
```

I can invoke a time-based `.rolling()` with a string for the offset:

```python
df.rolling('3d').sum()
```

I would like to invoke it with a `Timedelta` since it is easier to arithmetically change the window length:

```python
df.rolling(pd.Timedelta(days=3)).sum()
```


Discussion:
hmm, it allows offsets already (in fact internally that's all its doing), so this must be just an oversight

```
In [7]: df.rolling(pd.tseries.frequencies.to_offset(pd.Timedelta(days=3))).sum()
Out[7]: 
            value
2015-12-24    0.0
2015-12-25    1.0
2015-12-26    3.0
2015-12-27    6.0
2015-12-28    9.0
2015-12-29   12.0
2015-12-30   15.0
2015-12-31   18.0
2016-01-01   21.0
2016-01-02   24.0
```
Ah, I hadn't known about `to_offset()`! That's neat.
Thanks ! it would be great though if Timedelta were directly supported !
@fjanoos we *already* use ``to_offset`` internally

```
In [1]: from pandas.tseries.frequencies import to_offset

In [2]: to_offset(pd.offsets.Day(1)).nanos
Out[2]: 86400000000000

In [4]: to_offset(pd.Timedelta('1 day')).nanos
Out[4]: 86400000000000

```

this is just a matter of fixing the checking.

pull-requests are welcomed!

Resolving patch:
diff --git a/pandas/core/window.py b/pandas/core/window.py
--- a/pandas/core/window.py
+++ b/pandas/core/window.py
@@ -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,
@@ -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()


Test patch:
diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py
--- a/pandas/tests/test_window.py
+++ b/pandas/tests/test_window.py
@@ -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
@@ -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)