From f736ca28092a8fe34ba44bf484c7adc92a946eaf Mon Sep 17 00:00:00 2001 From: Peter Csizsek Date: Wed, 22 Feb 2017 17:19:45 +0100 Subject: [PATCH 1/5] The roll_quantile function in window.pyx now raises a ValueError when the quantile value is not in [0.0, 1.0] --- pandas/tests/test_window.py | 8 +++++++- pandas/window.pyx | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 452e8999ab13f..d7b0c3b9fa19c 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -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) @@ -1084,6 +1084,12 @@ def alt(x): self._check_moment_func(f, alt, name='quantile', quantile=q) + self.assertRaises(ValueError, mom.rolling_quantile, + np.array([1, 2, 3]), window=3, quantile=-0.1) + + self.assertRaises(ValueError, mom.rolling_quantile, + np.array([1, 2, 3]), window=3, quantile=10) + def test_rolling_apply(self): # suppress warnings about empty slices, as we are deliberately testing # with a 0-length Series diff --git a/pandas/window.pyx b/pandas/window.pyx index 8235d68e2a88b..005d42c9f68be 100644 --- a/pandas/window.pyx +++ b/pandas/window.pyx @@ -134,8 +134,8 @@ cdef class WindowIndexer: bint is_variable def get_data(self): - return (self.start, self.end, self.N, - self.win, self.minp, + return (self.start, self.end, self.N, + self.win, self.minp, self.is_variable) @@ -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( From f39b12212205941d025047415a158554ceff07af Mon Sep 17 00:00:00 2001 From: Peter Csizsek Date: Wed, 22 Feb 2017 18:01:05 +0100 Subject: [PATCH 2/5] Added a new test case to roll_quantile_test to trigger a TypeError when called with a string. --- pandas/tests/test_window.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index d7b0c3b9fa19c..ddc47f16ea2fd 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -1090,6 +1090,9 @@ def alt(x): self.assertRaises(ValueError, mom.rolling_quantile, np.array([1, 2, 3]), window=3, quantile=10) + self.assertRaises(TypeError, mom.rolling_quantile, + np.array([1, 2, 3]), window=3, quantile='foo') + def test_rolling_apply(self): # suppress warnings about empty slices, as we are deliberately testing # with a 0-length Series From 8b1e020684693be9ba13b6c1c50b5d6ec77bb2ee Mon Sep 17 00:00:00 2001 From: Peter Csizsek Date: Wed, 22 Feb 2017 18:13:43 +0100 Subject: [PATCH 3/5] Added a note about the Rolling.quantile bug fix to the changelog. --- doc/source/whatsnew/v0.20.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index 86f916bc0acfb..3b4f0695e8a36 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -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`) From 4eea34a7b929d8167a749aea4cd1bc2f778607fe Mon Sep 17 00:00:00 2001 From: Peter Csizsek Date: Wed, 22 Feb 2017 19:06:36 +0100 Subject: [PATCH 4/5] Refactored and moved exception throwing test to a new function for Rolling.quantile(). --- pandas/tests/test_window.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index ddc47f16ea2fd..a11e714a2ed0c 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -1084,14 +1084,15 @@ def alt(x): self._check_moment_func(f, alt, name='quantile', quantile=q) - self.assertRaises(ValueError, mom.rolling_quantile, - np.array([1, 2, 3]), window=3, quantile=-0.1) + def test_rolling_quantile_param(self): + ser = Series([0.0, .1, .5, .9, 1.0]) - self.assertRaises(ValueError, mom.rolling_quantile, - np.array([1, 2, 3]), window=3, quantile=10) + with self.assertRaises(ValueError): + ser.rolling(3).quantile(-0.1) + ser.rolling(3).quantile(10.0) - self.assertRaises(TypeError, mom.rolling_quantile, - np.array([1, 2, 3]), window=3, quantile='foo') + with self.assertRaises(TypeError): + ser.rolling(3).quantile('foo') def test_rolling_apply(self): # suppress warnings about empty slices, as we are deliberately testing From e31e5be062839244505ccbc3289c0be775052e46 Mon Sep 17 00:00:00 2001 From: Peter Csizsek Date: Wed, 22 Feb 2017 19:14:52 +0100 Subject: [PATCH 5/5] Correctly catching exception in the test for Rolling.quantile. --- pandas/tests/test_window.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index a11e714a2ed0c..3f2973a9834ca 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -1089,6 +1089,8 @@ def test_rolling_quantile_param(self): with self.assertRaises(ValueError): ser.rolling(3).quantile(-0.1) + + with self.assertRaises(ValueError): ser.rolling(3).quantile(10.0) with self.assertRaises(TypeError):