Skip to content

Commit

Permalink
BUG: Fix groupby quantile segfault (pandas-dev#27826)
Browse files Browse the repository at this point in the history
* BUG: Fix groupby quantile segfault

Validate that q is between 0 and 1.

Closes pandas-dev#27470

* prettier
  • Loading branch information
TomAugspurger authored and quintusdias committed Aug 15, 2019
1 parent 4fc595b commit a7dae1a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Groupby/resample/rolling

- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.transform` where applying a timezone conversion lambda function would drop timezone information (:issue:`27496`)
- Bug in windowing over read-only arrays (:issue:`27766`)
-
- Fixed segfault in `pandas.core.groupby.DataFrameGroupBy.quantile` when an invalid quantile was passed (:issue:`27470`)
-

Reshaping
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,11 @@ def group_quantile(ndarray[float64_t] out,
ndarray[int64_t] counts, non_na_counts, sort_arr

assert values.shape[0] == N

if not (0 <= q <= 1):
raise ValueError("'q' must be between 0 and 1. Got"
" '{}' instead".format(q))

inter_methods = {
'linear': INTERPOLATION_LINEAR,
'lower': INTERPOLATION_LOWER,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,17 @@ def test_quantile_raises():
df.groupby("key").quantile()


def test_quantile_out_of_bounds_q_raises():
# https://github.com/pandas-dev/pandas/issues/27470
df = pd.DataFrame(dict(a=[0, 0, 0, 1, 1, 1], b=range(6)))
g = df.groupby([0, 0, 0, 1, 1, 1])
with pytest.raises(ValueError, match="Got '50.0' instead"):
g.quantile(50)

with pytest.raises(ValueError, match="Got '-1.0' instead"):
g.quantile(-1)


# pipe
# --------------------------------

Expand Down

0 comments on commit a7dae1a

Please sign in to comment.