Skip to content

BUG: GroupBy.quantile implicitly sorts index.levels #53049

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

Merged
merged 14 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ Groupby/resample/rolling
the function operated on the whole index rather than each element of the index. (:issue:`51979`)
- Bug in :meth:`DataFrameGroupBy.apply` causing an error to be raised when the input :class:`DataFrame` was subset as a :class:`DataFrame` after groupby (``[['a']]`` and not ``['a']``) and the given callable returned :class:`Series` that were not all indexed the same. (:issue:`52444`)
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
- Bug in :meth:`GroupBy.quantile` may implicitly sort the result index with ``sort=False`` (:issue:`53009`)
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
-

Expand Down
15 changes: 12 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class providing the base-class of operations.
)
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.cast import ensure_dtype_can_hold_na
from pandas.core.dtypes.cast import (
coerce_indexer_dtype,
ensure_dtype_can_hold_na,
)
from pandas.core.dtypes.common import (
is_bool_dtype,
is_float_dtype,
Expand Down Expand Up @@ -4309,13 +4312,19 @@ def _insert_quantile_level(idx: Index, qs: npt.NDArray[np.float64]) -> MultiInde
MultiIndex
"""
nqs = len(qs)
lev_codes, lev = Index(qs).factorize()
lev_codes = coerce_indexer_dtype(lev_codes, lev)

if idx._is_multi:
idx = cast(MultiIndex, idx)
lev_codes, lev = Index(qs).factorize()
levels = list(idx.levels) + [lev]
codes = [np.repeat(x, nqs) for x in idx.codes] + [np.tile(lev_codes, len(idx))]
mi = MultiIndex(levels=levels, codes=codes, names=idx.names + [None])
else:
mi = MultiIndex.from_product([idx, qs])
nidx = len(idx)
idx_codes = coerce_indexer_dtype(np.arange(nidx), idx)
levels = [idx, lev]
codes = [np.repeat(idx_codes, nqs), np.tile(lev_codes, nidx)]
mi = MultiIndex(levels=levels, codes=codes, names=[idx.name, None])

return mi
30 changes: 30 additions & 0 deletions pandas/tests/groupby/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,33 @@ def test_groupby_quantile_dt64tz_period():
expected.index = expected.index.astype(np.int_)

tm.assert_frame_equal(result, expected)


def test_groupby_quantile_nonmulti_levels_order():
# Non-regression test for GH #53009
ind = pd.MultiIndex.from_tuples(
[
(0, "a", "B"),
(0, "a", "A"),
(0, "b", "B"),
(0, "b", "A"),
(1, "a", "B"),
(1, "a", "A"),
(1, "b", "B"),
(1, "b", "A"),
],
names=["sample", "cat0", "cat1"],
)
ser = pd.Series(range(8), index=ind)
result = ser.groupby(level="cat1", sort=False).quantile([0.2, 0.8])

qind = pd.MultiIndex.from_tuples(
[("B", 0.2), ("B", 0.8), ("A", 0.2), ("A", 0.8)], names=["cat1", None]
)
expected = pd.Series([1.2, 4.8, 2.2, 5.8], index=qind)

tm.assert_series_equal(result, expected)

# We need to check that index levels are not sorted
expected_levels = pd.core.indexes.frozen.FrozenList([["B", "A"], [0.2, 0.8]])
tm.assert_equal(result.index.levels, expected_levels)