Skip to content

BUG: stack with future_stack=True and empty list #60740

Closed
@behrenhoff

Description

@behrenhoff

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

df = pd.DataFrame()
old_working = df.stack(level=[])  # does nothing as list of levels is empty

new_broken = df.stack(level=[], future_stack=True)   # tries to stack despite the empty list

Issue Description

There is an unexpected behavioral change in stack with future_stack=True. The future_stack variant tries to stack even though the list of levels to stack is empty. The error message is ValueError: Cannot remove 1 levels from an index with 1 levels: at least one level must be left.

Expected Behavior

For me, this happens in code where the levels to stack are determined by a function - and the case of "empty list" seems to be forgotten in the new implementation. So could this behavior be restored in the new stack implementation?

Installed Versions

Tested with pandas 2.2.3.

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