Skip to content

Commit b94186d

Browse files
csizsekjreback
authored andcommitted
BUG: The roll_quantile function now throws an exception instead of causing a segfault when quantile is out of range
closes #15463 Author: Peter Csizsek <[email protected]> Closes #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]
1 parent 03eca9d commit b94186d

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

doc/source/whatsnew/v0.20.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ Bug Fixes
539539
- Bug in using ``__deepcopy__`` on empty NDFrame objects (:issue:`15370`)
540540
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a ``Series`` indexer (:issue:`14730`, :issue:`15424`)
541541
- Bug in ``DataFrame.loc`` with indexing a ``MultiIndex`` with a numpy array (:issue:`15434`)
542-
542+
- Bug in ``Rolling.quantile`` function that caused a segmentation fault when called with a quantile value outside of the range [0, 1] (:issue:`15463`)
543543

544544

545545
- Bug in the display of ``.info()`` where a qualifier (+) would always be displayed with a ``MultiIndex`` that contains only non-strings (:issue:`15245`)

pandas/tests/test_window.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ def test_rolling_max(self):
10631063
window=3, min_periods=5)
10641064

10651065
def test_rolling_quantile(self):
1066-
qs = [.1, .5, .9]
1066+
qs = [0.0, .1, .5, .9, 1.0]
10671067

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

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

1087+
def test_rolling_quantile_param(self):
1088+
ser = Series([0.0, .1, .5, .9, 1.0])
1089+
1090+
with self.assertRaises(ValueError):
1091+
ser.rolling(3).quantile(-0.1)
1092+
1093+
with self.assertRaises(ValueError):
1094+
ser.rolling(3).quantile(10.0)
1095+
1096+
with self.assertRaises(TypeError):
1097+
ser.rolling(3).quantile('foo')
1098+
10871099
def test_rolling_apply(self):
10881100
# suppress warnings about empty slices, as we are deliberately testing
10891101
# with a 0-length Series

pandas/window.pyx

+5-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ cdef class WindowIndexer:
134134
bint is_variable
135135

136136
def get_data(self):
137-
return (self.start, self.end, <int64_t>self.N,
138-
<int64_t>self.win, <int64_t>self.minp,
137+
return (self.start, self.end, <int64_t>self.N,
138+
<int64_t>self.win, <int64_t>self.minp,
139139
self.is_variable)
140140

141141

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

1288+
if quantile < 0.0 or quantile > 1.0:
1289+
raise ValueError("quantile value {0} not in [0, 1]".format(quantile))
1290+
12881291
# we use the Fixed/Variable Indexer here as the
12891292
# actual skiplist ops outweigh any window computation costs
12901293
start, end, N, win, minp, is_variable = get_window_indexer(

0 commit comments

Comments
 (0)