Skip to content

Commit c56744b

Browse files
committed
BUG: Fix quantile calculation pandas-dev#33200
1 parent 15a27ea commit c56744b

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

pandas/_libs/groupby.pyx

+4-7
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,9 @@ def group_quantile(ndarray[float64_t] out,
779779
non_na_counts[lab] += 1
780780

781781
# Get an index of values sorted by labels and then values
782-
sort_arr = np.arange(len(labels), dtype=np.int64)
783-
mask = labels != -1
784-
order = (np.asarray(values)[mask], labels[mask])
785-
sort_arr[mask] = np.lexsort(order).astype(np.int64, copy=False)
782+
labels[labels==-1] = np.max(labels) + 1
783+
order = (values, labels)
784+
sort_arr= np.lexsort(order).astype(np.int64, copy=False)
786785
with nogil:
787786
for i in range(ngroups):
788787
# Figure out how many group elements there are
@@ -820,9 +819,7 @@ def group_quantile(ndarray[float64_t] out,
820819

821820
# Increment the index reference in sorted_arr for the next group
822821
grp_start += grp_sz
823-
print(out)
824-
# out = np.roll(out, -(len(out) - np.sum(counts)))
825-
print(out)
822+
826823

827824
# ----------------------------------------------------------------------
828825
# group_nth, group_last, group_rank

pandas/tests/groupby/test_function.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,8 @@ def test_quantile_missing_group_values_no_segfaults():
15101510
@pytest.mark.parametrize(
15111511
"key, val, expected_key, expected_val",
15121512
[
1513-
([1.0, np.nan, 3.0, np.nan], range(4), [1.0, 3.0], [0.0, 1.0]),
1513+
([1.0, np.nan, 3.0, np.nan], range(4), [1.0, 3.0], [0.0, 2.0]),
1514+
([1.0, np.nan, 2.0, 2.0], range(4), [1.0, 2.0], [0.0, 2.5]),
15141515
(["a", "b", "b", np.nan], range(4), ["a", "b"], [0, 1.5]),
15151516
],
15161517
)

0 commit comments

Comments
 (0)