Skip to content

Commit 31260be

Browse files
TomAugspurgerWillAyd
authored andcommitted
Backport PR pandas-dev#27826 for 0.25.3 release
* BUG: Fix groupby quantile segfault Validate that q is between 0 and 1. Closes pandas-dev#27470 * prettier
1 parent 0efc71b commit 31260be

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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
@@ -1316,6 +1316,17 @@ def test_quantile_raises():
13161316
df.groupby("key").quantile()
13171317

13181318

1319+
def test_quantile_out_of_bounds_q_raises():
1320+
# https://github.com/pandas-dev/pandas/issues/27470
1321+
df = pd.DataFrame(dict(a=[0, 0, 0, 1, 1, 1], b=range(6)))
1322+
g = df.groupby([0, 0, 0, 1, 1, 1])
1323+
with pytest.raises(ValueError, match="Got '50.0' instead"):
1324+
g.quantile(50)
1325+
1326+
with pytest.raises(ValueError, match="Got '-1.0' instead"):
1327+
g.quantile(-1)
1328+
1329+
13191330
# pipe
13201331
# --------------------------------
13211332

0 commit comments

Comments
 (0)