Skip to content

Commit cdb725f

Browse files
authored
PERF: Groupby.quantile (#43725)
1 parent 5d21d13 commit cdb725f

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

pandas/core/groupby/groupby.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,10 +2655,9 @@ def post_processor(vals: np.ndarray, inference: np.dtype | None) -> np.ndarray:
26552655

26562656
return vals
26572657

2658-
if is_scalar(q):
2659-
res = self.quantile([q], interpolation=interpolation)
2660-
nlevels = res.index.nlevels
2661-
return res.droplevel(nlevels - 1, axis=0)
2658+
orig_scalar = is_scalar(q)
2659+
if orig_scalar:
2660+
q = [q]
26622661

26632662
qs = np.array(q, dtype=np.float64)
26642663
ids, _, ngroups = self.grouper.group_info
@@ -2729,6 +2728,9 @@ def blk_func(values: ArrayLike) -> ArrayLike:
27292728
else:
27302729
res = obj._constructor(res_mgr)
27312730

2731+
if orig_scalar:
2732+
# Avoid expensive MultiIndex construction
2733+
return self._wrap_aggregated_output(res)
27322734
return self._wrap_aggregated_output(res, qs=qs)
27332735

27342736
@final

0 commit comments

Comments
 (0)