Skip to content

Commit 26c9b2d

Browse files
committed
BUG: Rolling negative window issue fix #13383
1 parent b06bc7a commit 26c9b2d

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,4 @@ Bug Fixes
516516

517517

518518
- Bug in ``Categorical.remove_unused_categories()`` changes ``.codes`` dtype to platform int (:issue:`13261`)
519+
- Bug in ``Series.rolling()`` that allowed negative window, but failed on aggregation (:issue:`13383`)

pandas/core/window.py

+4
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ def validate(self):
321321
if isinstance(window, (list, tuple, np.ndarray)):
322322
pass
323323
elif com.is_integer(window):
324+
if window < 0:
325+
raise ValueError("window must be non-negative")
324326
try:
325327
import scipy.signal as sig
326328
except ImportError:
@@ -850,6 +852,8 @@ def validate(self):
850852
super(Rolling, self).validate()
851853
if not com.is_integer(self.window):
852854
raise ValueError("window must be an integer")
855+
elif self.window < 0:
856+
raise ValueError("window must be non-negative")
853857

854858
@Substitution(name='rolling')
855859
@Appender(SelectionMixin._see_also_template)

pandas/tests/test_window.py

+14
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ def test_constructor(self):
331331
c(window=2, min_periods=1, center=True)
332332
c(window=2, min_periods=1, center=False)
333333

334+
# GH 13383
335+
c(0)
336+
with self.assertRaises(ValueError):
337+
c(-1)
338+
334339
# not valid
335340
for w in [2., 'foo', np.array([2])]:
336341
with self.assertRaises(ValueError):
@@ -340,6 +345,15 @@ def test_constructor(self):
340345
with self.assertRaises(ValueError):
341346
c(window=2, min_periods=1, center=w)
342347

348+
def test_constructor_with_win_type(self):
349+
# GH 13383
350+
tm._skip_if_no_scipy()
351+
for o in [self.series, self.frame]:
352+
c = o.rolling
353+
c(0, win_type='boxcar')
354+
with self.assertRaises(ValueError):
355+
c(-1, win_type='boxcar')
356+
343357
def test_numpy_compat(self):
344358
# see gh-12811
345359
r = rwindow.Rolling(Series([2, 4, 6]), window=2)

0 commit comments

Comments
 (0)