Skip to content

BUG: DataFrame.stack with sort=True and unsorted MultiIndex levels #53636

Closed
@rhshadrach

Description

@rhshadrach

Ref: #53282 (comment)

levels = ((0, 1), (1, 0))
stack_lev = 1
columns = MultiIndex(levels=levels, codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
df = DataFrame(columns=columns, data=[range(4)])
df_stacked = df.stack(stack_lev, sort=True)
#      0  1
# 0 1  0  2
#   0  1  3

# Expected
#      0  1
# 0 0  1  3
#   1  0  2

When the MultiIndex levels are not sorted in ascending order, DataFrame.stack with sort=True does not sort the values in the result.

Metadata

Metadata

Assignees

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