Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 of 3 tasks
behrenhoff opened this issue Jan 20, 2025 · 3 comments · Fixed by #60826
Closed
2 of 3 tasks

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

behrenhoff opened this issue Jan 20, 2025 · 3 comments · Fixed by #60826
Assignees
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@behrenhoff
Copy link
Contributor

behrenhoff commented Jan 20, 2025

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.

@behrenhoff behrenhoff added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 20, 2025
@megha-66
Copy link

take

@rhshadrach
Copy link
Member

Thanks for the report! Agreed this should be a no-op.

@rhshadrach rhshadrach added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 22, 2025
@prakhar-bip
Copy link

prakhar-bip commented Feb 2, 2025

by default it takes one level at None, So I do this experiment and i get this

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=[-1], future_stack=True)   # tries to stack despite the empty list

print(new_broken)

Output

Series([], dtype: float64)

Now i am working on it , Soon i will make PR

If i am doing any mistake let me know

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants