Open kwhkim opened 12 months ago
@kwhkim - thanks for the report. Can you elaborate on this:
.stack().stack().stack() works but the result is not expected nor correct(the result is pd.Series)
What result do you get and what result do you expect?
@rhshadrach As I read the documentation for the method .unstack()
,
it looks like nothing wrong with .unstack().unstack().unstack()
according to the doc; confusion is mine.
I thought .unstack()
should be similar to .T
when the index is not MultiIndex...
And then again, the design looks somewhat different than what I would design, because,
dat = pd.DataFrame({'x':[1,3,2,4,5]})
dat.unstack() # works
dat.unstack().stack() # AttributeError!
dat.unstack().stack()
does not work for one-column DataFrame... I don't know if there is anthoer deep understanding about how .unstack()
or .stack()
should do...
.unstack() does not work for triple(?) MultiIndex.
Agreed - this looks like a bug to me. Further investigations and PRs to fix are welcome. This is a simpler reproducer.
df = pd.DataFrame({'a': [1, 1, 2], 'b': [1, 2, 1], 'c': [3, 4, 5]}).set_index(['a', 'b'])
df['c'].unstack([0, 1])
And then again, the design looks somewhat different than what I would design, because,
I do not know what you are trying to say here. How would you design it?
dat.unstack().stack() # AttributeError!
You are trying to call .stack()
on a Series. This method is not implemented for a Series.
Yes, I am just wondering if dat.unstack()
resulting in a Series looks appropriate design-wise,
which breaks the rule of dat.unstack().stack()
results in the original dat
.
take
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
.unstack()
does not work for triple(?) MultiIndex..stack().stack().stack()
works but the result is not expected nor correct(the result is pd.Series)Expected Behavior
.unstack([0,1,2])
for triple MultiIndex should produce 1-row DataFrame with triple MultiIndex for columnsInstalled Versions