BUG: stack with future_stack=True and empty list · Issue #60740 · pandas-dev/pandas (original) (raw)

Pandas version checks

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.