Skip to content

Commit 5fea18d

Browse files
committed
BUG: GroupBy.quantile error with integer columns and arraylike q (GH30289)
1 parent d207a3e commit 5fea18d

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ Groupby/resample/rolling
846846
- Bug in :meth:`DataFrameGroupBy.agg` with timezone-aware datetime64 column incorrectly casting results to the original dtype (:issue:`29641`)
847847
- Bug in :meth:`DataFrame.groupby` when using axis=1 and having a single level columns index (:issue:`30208`)
848848
- Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`)
849+
- Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names
849850

850851
Reshaping
851852
^^^^^^^^^

pandas/core/groupby/groupby.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -1937,21 +1937,18 @@ def post_processor(vals: np.ndarray, inference: Optional[Type]) -> np.ndarray:
19371937
# >>> result.stack(0).loc[pd.IndexSlice[:, ..., q], :]
19381938
# but this hits https://github.com/pandas-dev/pandas/issues/10710
19391939
# which doesn't reorder the list-like `q` on the inner level.
1940-
order = np.roll(list(range(result.index.nlevels)), -1)
1940+
order = np.append(np.arange(1, result.index.nlevels), 0)
1941+
# temporarily saves the index names
1942+
index_names = np.array(result.index.names)
1943+
# set index names to positions to avoid confusion
1944+
result.index.names = np.arange(len(index_names))
1945+
# place quantiles on the inside
19411946
result = result.reorder_levels(order)
1942-
result = result.reindex(q, level=-1)
1947+
# restore the index names in order
1948+
result.index.names = index_names[order]
19431949

1944-
# fix order.
1945-
hi = len(q) * self.ngroups
1946-
arr = np.arange(0, hi, self.ngroups)
1947-
arrays = []
1948-
1949-
for i in range(self.ngroups):
1950-
arr2 = arr + i
1951-
arrays.append(arr2)
1952-
1953-
indices = np.concatenate(arrays)
1954-
assert len(indices) == len(result)
1950+
# reorder rows to keep things sorted
1951+
indices = np.arange(len(result)).reshape([len(q), self.ngroups]).T.flatten()
19551952
return result.take(indices)
19561953

19571954
@Substitution(name="groupby")

0 commit comments

Comments
 (0)