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

Conversation

csizsek
Copy link
Contributor

@csizsek csizsek commented Feb 22, 2017

The roll_quantile function in window.pyx now raises a ValueError when the quantile value is not in [0.0, 1.0]. Also added tests for the new exception and extended test cases for the roll_quantile function.

@jreback
Copy link
Contributor

jreback commented Feb 22, 2017

@csizsek great! please add a whatsnew note in Bug Fixes (0.20.0). otherwise lgtm. (FYI, you can also tests this (already works, but just to have a test)

In [2]: Series([1,2,3]).rolling(2).quantile('foo')
TypeError: a float is required

@jreback jreback added Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Error Reporting Incorrect or improved errors from pandas labels Feb 22, 2017
@jreback jreback added this to the 0.20.0 milestone Feb 22, 2017
@csizsek
Copy link
Contributor Author

csizsek commented Feb 22, 2017

@jreback Thanks for the quick review and the help, I added the test you suggested, but I'm not sure how to do this: "please add a whatsnew note in Bug Fixes (0.20.0)". What file do I need to modify?

@jreback
Copy link
Contributor

jreback commented Feb 22, 2017

https://github.com/pandas-dev/pandas/blob/master/doc/source/whatsnew/v0.20.0.txt

use a blank line in the Bug Fix section (not at the end)

@csizsek
Copy link
Contributor Author

csizsek commented Feb 22, 2017

Thanks, done.

Do I need to do anything else or can we consider this as fixed?

@jreback
Copy link
Contributor

jreback commented Feb 22, 2017

next step is to ping on green. lgtm otherwise though.

@@ -1084,6 +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)
Copy link
Contributor

Choose a reason for hiding this comment

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

actually make this a separate function, and don't use an array, instead use the new format

Series(....).rolling(3). We are dropping pd.rolling_quantile soon anyhow (as its been deprecated for a while).

Copy link
Contributor

Choose a reason for hiding this comment

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

To avoid extra lambdas, can use:

with self.assertRaises(ValueError):
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for the feedback, fixed.

@@ -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

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)
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

@csizsek
Copy link
Contributor Author

csizsek commented Feb 22, 2017

@kernc @jreback All green.

@codecov-io
Copy link

Codecov Report

Merging #15476 into master will increase coverage by <.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master   #15476      +/-   ##
==========================================
+ Coverage   90.35%   90.36%   +<.01%     
==========================================
  Files         135      135              
  Lines       49513    49519       +6     
==========================================
+ Hits        44739    44747       +8     
+ Misses       4774     4772       -2
Impacted Files Coverage Δ
pandas/indexes/category.py 98.59% <ø> (ø)
pandas/types/concat.py 98.06% <ø> (+0.01%)
pandas/core/categorical.py 96.87% <ø> (+0.02%)
pandas/core/groupby.py 94.98% <ø> (+0.09%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 486e384...e31e5be. Read the comment docs.

@jreback jreback closed this in b94186d Feb 23, 2017
@jreback
Copy link
Contributor

jreback commented Feb 23, 2017

thanks @csizsek !

keep em coming!

@csizsek
Copy link
Contributor Author

csizsek commented Feb 23, 2017

Sure. Thanks for the help!

AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this pull request Mar 21, 2017
…using a segfault when quantile is out of range

closes pandas-dev#15463

Author: Peter Csizsek <[email protected]>

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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Algos Non-arithmetic algos: value_counts, factorize, sorting, isin, clip, shift, diff Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Series.rolling(3).quantile(10) segmentation fault
4 participants