Skip to content

The roll_quantile function now throws an exception instead of causing a segfault #15476

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
Closed
Show file tree
Hide file tree
Changes from 4 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
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 @@ -504,7 +504,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
12 changes: 11 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,16 @@ 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)
ser.rolling(3).quantile(10.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

The second call needs it's own with block, otherwise the first call's risen ValueError might (in theory) mask the second one's, or vice versa.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed :D


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:
Copy link
Contributor

@kernc kernc Feb 22, 2017

Choose a reason for hiding this comment

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

Can use a single 0 <= quantile <= 1 expression. Mind the endpoints: can't the chosen quantile be 0 or 1?

Oh, never mind. 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it can, please see the PR

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