Skip to content

Commit 0f351dc

Browse files
priyankjainjreback
authored andcommitted
BUG: Rolling negative window issue fix #13383
closes #13383 Author: priyankjain <[email protected]> Closes #13441 from priyankjain/novice-bug-fixes and squashes the following commits: 26c9b2d [priyankjain] BUG: Rolling negative window issue fix #13383
1 parent 3b75e03 commit 0f351dc

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
@@ -480,6 +480,7 @@ Bug Fixes
480480
- Bug in ``groupby(..).resample(..)`` where passing some keywords would raise an exception (:issue:`13235`)
481481
- Bug in ``.tz_convert`` on a tz-aware ``DateTimeIndex`` that relied on index being sorted for correct results (:issue:`13306`)
482482
- Bug in ``pd.read_hdf()`` where attempting to load an HDF file with a single dataset, that had one or more categorical columns, failed unless the key argument was set to the name of the dataset. (:issue:`13231`)
483+
- Bug in ``.rolling()`` that allowed a negative integer window in contruction of the ``Rolling()`` object, but would later fail on aggregation (:issue:`13383`)
483484

484485

485486
- Bug in various index types, which did not propagate the name of passed index (:issue:`12309`)

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)