Skip to content

Commit

Permalink
BUG: Maintain the order of the bins in group_quantile. Updated tests p…
Browse files Browse the repository at this point in the history
  • Loading branch information
mabelvj committed Apr 19, 2020
1 parent c8db9b9 commit 7f94c20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -779,9 +779,10 @@ def group_quantile(ndarray[float64_t] out,
non_na_counts[lab] += 1

# Get an index of values sorted by labels and then values
order = (values, labels)
sort_arr = np.lexsort(order).astype(np.int64, copy=False)

sort_arr = np.arange(len(labels), dtype=np.int64)
mask = labels != -1
order = (np.asarray(values)[mask], labels[mask])
sort_arr[mask] = np.lexsort(order).astype(np.int64, copy=False)
with nogil:
for i in range(ngroups):
# Figure out how many group elements there are
Expand Down
18 changes: 13 additions & 5 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,14 +1507,22 @@ def test_quantile_missing_group_values_no_segfaults():
grp.quantile()


def test_quantile_missing_group_values_correct_results():
# GH 28662
data = np.array([1.0, np.nan, 3.0, np.nan])
df = pd.DataFrame(dict(key=data, val=range(4)))
@pytest.mark.parametrize(
"key, val, expected_key, expected_val",
[
([1.0, np.nan, 3.0, np.nan], range(4), [1.0, 3.0], [0.0, 1.0]),
(["a", "b", "b", np.nan], range(4), ["a", "b"], [0, 1.5]),
],
)
def test_quantile_missing_group_values_correct_results(
key, val, expected_key, expected_val
):
# GH 28662, GH 33200
df = pd.DataFrame({"key": key, "val": val})

result = df.groupby("key").quantile()
expected = pd.DataFrame(
[1.0, 3.0], index=pd.Index([1.0, 3.0], name="key"), columns=["val"]
expected_val, index=pd.Index(expected_key, name="key"), columns=["val"]
)
tm.assert_frame_equal(result, expected)

Expand Down

0 comments on commit 7f94c20

Please sign in to comment.