Skip to content

BUG: .unstack(sort=False) reorders columns labels without the column values #62816

@aallahyar

Description

@aallahyar

Pandas version checks

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

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

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

Reproducible Example

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    "Department": ["HR", "HR", "Finance", "Finance", "IT", "IT", "Finance", "HR", "IT", "Finance"],
    "Gender": ["F", "M", "M", "F", "M", "F", "M", "F", "M", "F"],
    "Location": ["NY", "CA", "CA", "NY", "CA", "NY", "CA", "CA", "CA", "NY"]
})

# Counts combinations of three columns, but wrong column labels assigned:
print(
    df
    .value_counts(subset=["Department", "Gender", "Location"])
    .unstack(fill_value=0, sort=False)
)

# output:
Location           CA  NY
Department Gender        
Finance    F        0   2
           M        2   0
HR         F        1   1
           M        1   0
IT         F        0   1
           M        2   0

Issue Description

In Pandas (v2.3), I have noticed an issue with .unstack(fill_value=0, sort=False) where incorrect column labels are assigned to column values.

For example, if you add the sort=False, the counts are not correct:

print(
    df
    .value_counts(subset=["Department", "Gender", "Location"])
    .unstack(fill_value=0, sort=False)
)

The output is:

Location           NY  CA
Department Gender        
Finance    F        0   2
           M        2   0
IT         M        2   1
HR         F        1   0
           M        0   1
IT         F        1   0

Note that the dataframe contains no female in the Finnance department in CA.

I have seen related bug reports (#55516 and #53910) where this argument is supposed to be deprecated. But it seems that no warning is being issued for the deprecation.

Expected Behavior

The following works as expected:

# note: use the above "reproducible" code to generate the `df`
print(
    df
    .value_counts(subset=["Department", "Gender", "Location"])
    .unstack(fill_value=0) # without sort argument
)

output:

Location           CA  NY
Department Gender        
Finance    F        0   2
           M        2   0
HR         F        1   1
           M        1   0
IT         F        0   1
           M        2   0

Installed Versions

INSTALLED VERSIONS

commit : 2cc3762
python : 3.12.7
python-bits : 64
OS : Linux
OS-release : 4.18.0-372.32.1.el8_6.x86_64
Version : #1 SMP Thu Oct 27 15:18:36 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.3.0
numpy : 2.2.6
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : None
sphinx : None
IPython : 9.3.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.4
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.3
numba : 0.61.2
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.2
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions