Skip to content

BUG: Rolling negative window issue fix #13383 #13441

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

Closed
wants to merge 1 commit into from
Closed
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.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
4 changes: 4 additions & 0 deletions pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

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

there is a another place where this is checked. in _Window (so need a test for that as well)

raise ValueError("window must be non-negative")

@Substitution(name='rolling')
@Appender(SelectionMixin._see_also_template)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down