Skip to content
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

BUG: quantile segfaults on invalid quantile values #27473

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,10 @@ 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("Quantile values must lie in the interval [0, 1]")

inter_methods = {
'linear': INTERPOLATION_LINEAR,
'lower': INTERPOLATION_LOWER,
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,18 @@ def test_groupby_transform_timezone_column(func):
tm.assert_frame_equal(result, expected)


def test_quantile_validation():
This conversation was marked as resolved.
Show resolved Hide resolved
# GH#27470
df = pd.DataFrame(dict(a=[0, 0, 0, 1, 1, 1]))
g = df.groupby(np.repeat([0, 1], 3))
result = g.quantile(0.5)
expected = DataFrame(dict(a=[0.0, 1.0]))
tm.assert_frame_equal(result, expected)
This conversation was marked as resolved.
Show resolved Hide resolved

with pytest.raises(ValueError, match="uantile values"):
g.quantile(1.1)


@pytest.mark.parametrize(
"func, values",
[
Expand Down