Skip to content
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

BUG: pandas.DataFrame().stack() raise an error, while expected is empty #36113

Closed
2 of 3 tasks
evyasonov opened this issue Sep 4, 2020 · 5 comments · Fixed by #36185
Closed
2 of 3 tasks

BUG: pandas.DataFrame().stack() raise an error, while expected is empty #36113

evyasonov opened this issue Sep 4, 2020 · 5 comments · Fixed by #36185
Labels
Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@evyasonov
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

pandas.DataFrame().stack()
pandas.DataFrame().stack(dropna = True)

Problem description

I expect that stack() of empty dataframe will be empty dataframe and not an exception

@evyasonov evyasonov added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2020
@TomAugspurger
Copy link
Contributor

TomAugspurger commented Sep 4, 2020

Thanks for the report. I think the expected output should be an empty Series rather than an empty DataFrame. What do you think?

@TomAugspurger TomAugspurger added Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2020
@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Sep 4, 2020
@evyasonov
Copy link
Author

@TomAugspurger
I expect a dataframe, because if input is not empty, then the method returns a dataframe

@TomAugspurger
Copy link
Contributor

Doesn't DataFrame.stack return a Series? Or do I misunderstand?

@ivanovmg
Copy link
Member

ivanovmg commented Sep 8, 2020

From the code on stack method:

from pandas.core.reshape.reshape import stack, stack_multiple

if isinstance(level, (tuple, list)):
    return stack_multiple(self, level, dropna=dropna)
else:
    return stack(self, level, dropna=dropna)

By default level=-1.
Meanwhile pandas.core.reshape.reshape.stack returns a Series.

@evyasonov
Copy link
Author

@TomAugspurger
All of my use cases were about multiindex, so .stack() returns a dataframe for my cases.
But I checked the documentation and it returns a Series if it's just one level.
So let's use the logic @ivanovmg described

@jreback jreback modified the milestones: Contributions Welcome, 1.2 Sep 11, 2020
@jreback jreback modified the milestones: 1.2, Contributions Welcome Nov 24, 2020
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