Skip to content

Commit 96d8471

Browse files
TomAugspurgerMeeseeksDev[bot]
authored and
MeeseeksDev[bot]
committed
Backport PR pandas-dev#28113: BUG: Fix groupby quantile array
1 parent 4a307f6 commit 96d8471

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/source/whatsnew/v0.25.2.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ Plotting
7676
Groupby/resample/rolling
7777
^^^^^^^^^^^^^^^^^^^^^^^^
7878

79-
-
80-
-
79+
- Bug incorrectly raising an ``IndexError`` when passing a list of quantiles to :meth:`pandas.core.groupby.DataFrameGroupBy.quantile` (:issue:`28113`).
8180
-
8281
-
8382
-

pandas/core/groupby/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1945,8 +1945,8 @@ def post_processor(vals: np.ndarray, inference: Optional[Type]) -> np.ndarray:
19451945
arrays = []
19461946

19471947
for i in range(self.ngroups):
1948-
arr = arr + i
1949-
arrays.append(arr)
1948+
arr2 = arr + i
1949+
arrays.append(arr2)
19501950

19511951
indices = np.concatenate(arrays)
19521952
assert len(indices) == len(result)

pandas/tests/groupby/test_function.py

+18
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,24 @@ def test_quantile_array():
12571257
tm.assert_frame_equal(result, expected)
12581258

12591259

1260+
def test_quantile_array2():
1261+
# https://github.com/pandas-dev/pandas/pull/28085#issuecomment-524066959
1262+
df = pd.DataFrame(
1263+
np.random.RandomState(0).randint(0, 5, size=(10, 3)), columns=list("ABC")
1264+
)
1265+
result = df.groupby("A").quantile([0.3, 0.7])
1266+
expected = pd.DataFrame(
1267+
{
1268+
"B": [0.9, 2.1, 2.2, 3.4, 1.6, 2.4, 2.3, 2.7, 0.0, 0.0],
1269+
"C": [1.2, 2.8, 1.8, 3.0, 0.0, 0.0, 1.9, 3.1, 3.0, 3.0],
1270+
},
1271+
index=pd.MultiIndex.from_product(
1272+
[[0, 1, 2, 3, 4], [0.3, 0.7]], names=["A", None]
1273+
),
1274+
)
1275+
tm.assert_frame_equal(result, expected)
1276+
1277+
12601278
def test_quantile_array_no_sort():
12611279
df = pd.DataFrame({"A": [0, 1, 2], "B": [3, 4, 5]})
12621280
result = df.groupby([1, 0, 1], sort=False).quantile([0.25, 0.5, 0.75])

0 commit comments

Comments
 (0)