Skip to content

BUG: unstack with sort=False fails when used with the level parameter #54987

Closed
@m-amin-alavian

Description

@m-amin-alavian

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

index = pd.MultiIndex.from_tuples(
    [
        (2020, 'Urban'),
        (2020, 'Rural',),
        (2021, 'Urban'),
        (2021, 'Rural',),
    ],
    names=["Year", "Urban_Rural"]
)
data = pd.Series(
    [21, 11, 22, 12],
    index=index
)

# sort = True:
data.unstack(0)

# | Urban_Rural   |   2020 |   2021 |
# |:--------------|-------:|-------:|
# | Rural         |     11 |     12 |
# | Urban         |     21 |     22 |


# sort = False
data.unstack(0, sort=False)

# | Urban_Rural   |   2020 |   2021 |
# |:--------------|-------:|-------:|
# | Urban         |     21 |     11 |
# | Rural         |     22 |     12 |


# sort = False without level
data.unstack(sort=False)

# |   Year |   Urban |   Rural |
# |-------:|--------:|--------:|
# |   2020 |      21 |      11 |
# |   2021 |      22 |      12 |

Issue Description

The issue occurs when using the unstack method in pandas with the sort=False parameter and a specified level. In this scenario, the values within the resulting DataFrame are not affected by the specified level and are displayed as if the level is set to its default value, -1. However, the labels are correctly applied, leading to an incorrect resulting DataFrame.

Expected Behavior

When using unstack with level and sort=False, the resulting DataFrame should correctly reflect the values from the input data.

Installed Versions

INSTALLED VERSIONS

commit : 4b456e2
python : 3.11.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22621
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.0dev0+171.g4b456e232
numpy : 1.25.2
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.2.1
Cython : None
pytest : 7.4.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader : None
bs4 : None
bottleneck : 1.3.7
dataframe-api-compat: None
fastparquet : 2023.8.0
fsspec : 2023.6.0
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : 2.8.5
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

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