Skip to content

Commit 9fd432b

Browse files
TomAugspurgerjbrockmendel
authored andcommitted
BUG: Fix groupby quantile segfault (#27826)
* BUG: Fix groupby quantile segfault Validate that q is between 0 and 1. Closes #27470 * prettier
1 parent 2ebab98 commit 9fd432b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

doc/source/whatsnew/v0.25.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Groupby/resample/rolling
120120

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

126126
Reshaping

pandas/_libs/groupby.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,11 @@ def group_quantile(ndarray[float64_t] out,
719719
ndarray[int64_t] counts, non_na_counts, sort_arr
720720

721721
assert values.shape[0] == N
722+
723+
if not (0 <= q <= 1):
724+
raise ValueError("'q' must be between 0 and 1. Got"
725+
" '{}' instead".format(q))
726+
722727
inter_methods = {
723728
'linear': INTERPOLATION_LINEAR,
724729
'lower': INTERPOLATION_LOWER,

pandas/tests/groupby/test_function.py

+11
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,17 @@ def test_quantile_raises():
12471247
df.groupby("key").quantile()
12481248

12491249

1250+
def test_quantile_out_of_bounds_q_raises():
1251+
# https://github.com/pandas-dev/pandas/issues/27470
1252+
df = pd.DataFrame(dict(a=[0, 0, 0, 1, 1, 1], b=range(6)))
1253+
g = df.groupby([0, 0, 0, 1, 1, 1])
1254+
with pytest.raises(ValueError, match="Got '50.0' instead"):
1255+
g.quantile(50)
1256+
1257+
with pytest.raises(ValueError, match="Got '-1.0' instead"):
1258+
g.quantile(-1)
1259+
1260+
12501261
# pipe
12511262
# --------------------------------
12521263

0 commit comments

Comments
 (0)