From 26c9b2d2f57a3d474ea33baba28e6e83509baf22 Mon Sep 17 00:00:00 2001 From: priyankjain Date: Sun, 12 Jun 2016 19:59:09 +0530 Subject: [PATCH] BUG: Rolling negative window issue fix #13383 --- doc/source/whatsnew/v0.18.2.txt | 1 + pandas/core/window.py | 4 ++++ pandas/tests/test_window.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index 6bc152aad6b01..0f8ed0558b5f1 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -516,3 +516,4 @@ Bug Fixes - Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`) +- Bug in ``Series.rolling()`` that allowed negative window, but failed on aggregation (:issue:`13383`) diff --git a/pandas/core/window.py b/pandas/core/window.py index fbc56335aabd9..1e34d18fe3e54 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -321,6 +321,8 @@ def validate(self): if isinstance(window, (list, tuple, np.ndarray)): pass elif com.is_integer(window): + if window < 0: + raise ValueError("window must be non-negative") try: import scipy.signal as sig except ImportError: @@ -850,6 +852,8 @@ def validate(self): super(Rolling, self).validate() if not com.is_integer(self.window): raise ValueError("window must be an integer") + elif self.window < 0: + raise ValueError("window must be non-negative") @Substitution(name='rolling') @Appender(SelectionMixin._see_also_template) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 2ec419221c6d8..3693ebdb12e2f 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -331,6 +331,11 @@ def test_constructor(self): c(window=2, min_periods=1, center=True) c(window=2, min_periods=1, center=False) + # GH 13383 + c(0) + with self.assertRaises(ValueError): + c(-1) + # not valid for w in [2., 'foo', np.array([2])]: with self.assertRaises(ValueError): @@ -340,6 +345,15 @@ def test_constructor(self): with self.assertRaises(ValueError): c(window=2, min_periods=1, center=w) + def test_constructor_with_win_type(self): + # GH 13383 + tm._skip_if_no_scipy() + for o in [self.series, self.frame]: + c = o.rolling + c(0, win_type='boxcar') + with self.assertRaises(ValueError): + c(-1, win_type='boxcar') + def test_numpy_compat(self): # see gh-12811 r = rwindow.Rolling(Series([2, 4, 6]), window=2)