Skip to content

Commit 7d2b2c5

Browse files
committed
Backport PR pandas-dev#29173: Fixed segfaults and incorrect results in GroupBy.quantile with NA Values in Grouping
1 parent 0efc71b commit 7d2b2c5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pandas/_libs/groupby.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ def group_quantile(ndarray[float64_t] out,
736736
with nogil:
737737
for i in range(N):
738738
lab = labels[i]
739+
if lab == -1: # NA group label
740+
continue
741+
739742
counts[lab] += 1
740743
if not mask[i]:
741744
non_na_counts[lab] += 1

pandas/tests/groupby/test_function.py

+34
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,40 @@ 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+
1330+
def test_quantile_missing_group_values_no_segfaults():
1331+
# GH 28662
1332+
data = np.array([1.0, np.nan, 1.0])
1333+
df = pd.DataFrame(dict(key=data, val=range(3)))
1334+
1335+
# Random segfaults; would have been guaranteed in loop
1336+
grp = df.groupby("key")
1337+
for _ in range(100):
1338+
grp.quantile()
1339+
1340+
1341+
def test_quantile_missing_group_values_correct_results():
1342+
# GH 28662
1343+
data = np.array([1.0, np.nan, 3.0, np.nan])
1344+
df = pd.DataFrame(dict(key=data, val=range(4)))
1345+
1346+
result = df.groupby("key").quantile()
1347+
expected = pd.DataFrame(
1348+
[1.0, 3.0], index=pd.Index([1.0, 3.0], name="key"), columns=["val"]
1349+
)
1350+
tm.assert_frame_equal(result, expected)
1351+
1352+
13191353
# pipe
13201354
# --------------------------------
13211355

0 commit comments

Comments
 (0)