Skip to content

Commit

Permalink
BUG: The roll_quantile function now throws an exception instead of ca…
Browse files Browse the repository at this point in the history
…using a segfault when quantile is out of range

closes pandas-dev#15463

Author: Peter Csizsek <peter.csizsek@gmail.com>

Closes pandas-dev#15476 from csizsek/fix-rolling-quantile-segfault and squashes the following commits:

e31e5be [Peter Csizsek] Correctly catching exception in the test for Rolling.quantile.
4eea34a [Peter Csizsek] Refactored and moved exception throwing test to a new function for Rolling.quantile().
8b1e020 [Peter Csizsek] Added a note about the Rolling.quantile bug fix to the changelog.
f39b122 [Peter Csizsek] Added a new test case to roll_quantile_test to trigger a TypeError when called with a string.
f736ca2 [Peter Csizsek] The roll_quantile function in window.pyx now raises a ValueError when the quantile value is not in [0.0, 1.0]
  • Loading branch information
csizsek authored and jreback committed Feb 23, 2017
1 parent 03eca9d commit b94186d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ Bug Fixes
- Bug in using ``__deepcopy__`` on empty NDFrame objects (:issue:`15370`)
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`, :issue:`15424`)
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a numpy array (:issue:`15434`)

- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)


- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ def test_rolling_max(self):
window=3, min_periods=5)

def test_rolling_quantile(self):
qs = [.1, .5, .9]
qs = [0.0, .1, .5, .9, 1.0]

def scoreatpercentile(a, per):
values = np.sort(a, axis=0)
Expand All @@ -1084,6 +1084,18 @@ def alt(x):

self._check_moment_func(f, alt, name='quantile', quantile=q)

def test_rolling_quantile_param(self):
ser = Series([0.0, .1, .5, .9, 1.0])

with self.assertRaises(ValueError):
ser.rolling(3).quantile(-0.1)

with self.assertRaises(ValueError):
ser.rolling(3).quantile(10.0)

with self.assertRaises(TypeError):
ser.rolling(3).quantile('foo')

def test_rolling_apply(self):
# suppress warnings about empty slices, as we are deliberately testing
# with a 0-length Series
Expand Down
7 changes: 5 additions & 2 deletions pandas/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ cdef class WindowIndexer:
bint is_variable

def get_data(self):
return (self.start, self.end, <int64_t>self.N,
<int64_t>self.win, <int64_t>self.minp,
return (self.start, self.end, <int64_t>self.N,
<int64_t>self.win, <int64_t>self.minp,
self.is_variable)


Expand Down Expand Up @@ -1285,6 +1285,9 @@ def roll_quantile(ndarray[float64_t, cast=True] input, int64_t win,
ndarray[int64_t] start, end
ndarray[double_t] output

if quantile < 0.0 or quantile > 1.0:
raise ValueError("quantile value {0} not in [0, 1]".format(quantile))

# we use the Fixed/Variable Indexer here as the
# actual skiplist ops outweigh any window computation costs
start, end, N, win, minp, is_variable = get_window_indexer(
Expand Down

0 comments on commit b94186d

Please sign in to comment.