Pandas version checks

  • [x] I have checked that this issue has not already been reported.

  • [x] I have confirmed this bug exists on the latest version of pandas.

  • [x] I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

multi_index = pd.MultiIndex.from_tuples([
    ("A", "1"),
    ("A", "2"),
    ("B", "1"),
    ("B", "2"),
    ("C", "1"),
    ("C", "2"),
    ("A", "3"),
    ("B", "3"),
    ("C", "3"),
], names=['level_1', 'level_2'])

values = [f"{l1} {l2}" for l1, l2 in multi_index]
series = pd.Series(values, index=multi_index, name='Example')

series.groupby(['level_1', 'level_2'], sort=False).sum().unstack('level_2', sort=False)

Issue Description

The code above generates this output:

| level_1   | 1   | 2   | 3   |
|:----------|:----|:----|:----|
| A         | A 1 | A 2 | B 1 |
| B         | B 2 | C 1 | C 2 |
| C         | A 3 | B 3 | C 3 |

where the values are not placed correctly in their respective columns and indices. For example, the value B 1 can only belong to the row B and column 1, but here it appears in A3.

Expected Behavior

The issue disappears if any of the sort flags is set to True. Namely, replacing the last line with any of these 3 lines returns the expected output:

  • series.groupby(['level_1', 'level_2'], sort=True).sum().unstack('level_2', sort=False).
  • series.groupby(['level_1', 'level_2'], sort=True).sum().unstack('level_2', sort=True).
  • series.groupby(['level_1', 'level_2'], sort=False).sum().unstack('level_2', sort=True).

The issue only seems to appear when both groupby and unstack have sort=False.

Installed Versions

INSTALLED VERSIONS ------------------ commit : 4665c10899bc413b639194f6fb8665a5c70f7db5 python : 3.11.11 python-bits : 64 OS : Darwin OS-release : 24.6.0 Version : Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:51 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T8112 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : None LOCALE : en_US.UTF-8 pandas : 2.3.2 numpy : 2.3.2 pytz : 2025.2 dateutil : 2.9.0.post0 pip : 25.2 Cython : None sphinx : None IPython : 9.4.0 adbc-driver-postgresql: None adbc-driver-sqlite : None bs4 : None blosc : None bottleneck : None dataframe-api-compat : None fastparquet : None fsspec : None html5lib : None hypothesis : None gcsfs : None jinja2 : 3.1.6 lxml.etree : None matplotlib : 3.10.5 numba : None numexpr : None odfpy : None openpyxl : 3.1.5 pandas_gbq : None psycopg2 : None pymysql : None pyarrow : None pyreadstat : None pytest : 8.4.1 python-calamine : None pyxlsb : None s3fs : None scipy : None sqlalchemy : 2.0.43 tables : None tabulate : 0.9.0 xarray : None xlrd : None xlsxwriter : None zstandard : 0.24.0 tzdata : 2025.2 qtpy : None pyqt5 : None

Comment From: JuanseHevia

Hi, is there a fix for this? I'm encountering the same issue