Code Sample, a copy-pastable example if possible
>>> import pandas as pd
>>> # Notice that the columns are not sorted below.
>>> df = pd.DataFrame(data=[[0,1],[2,3],[4,5],[6,7]],index = pd.MultiIndex.from_product([['a','b'],['A','B']]),columns=['d','c'])
>>> # The value of the element with indices 'b', 'B', and 'd'
>>> df.loc[('b','B'),'d']
6
>>> # The value of that *same* element now.
>>> df.unstack().stack(0).loc[('b','d'),'B']
7
>>> # What went wrong?
>>> df
d c
a A 0 1
B 2 3
b A 4 5
B 6 7
>>> # During some step, the indices got sorted but the values did not follow.
>>> df.unstack().stack(0)
A B
a d 1 3
c 0 2
b d 5 7
c 4 6
Problem description
With MultiIndex
ed DataFrames
, it becomes convenient to unstack(level)
and stack(level)
your DataFrame until it has the indices you need to do what you want to do. These methods will sort your indices or levels if they were not sorted to begin with.
However, apparently I have discovered a case where the indices got sorted, but the values did not follow, resulting in the "shuffling" you see above.
Expected Output
The expected behavior is that these operations should not result in data scrambling / shuffling; a complete set of indices (like {'b','B','d'}) should always refer to the same value (in this case, 6).
Output of pd.show_versions()
Comment From: gfyoung
@joseortiz3 : Thanks for reporting! One thing that we suggest users do is upgrade if possible to the latest version, as we may have already resolved the issue.
I can't reproduce this in 0.20.3
(latest). Can you upgrade and see if you can still reproduce?
Comment From: joseortiz3
Oh jeez my bad. One sec.
Yes, it has already been fixed. My bad.
Comment From: gfyoung
No worries! This was only added to the issue template recently. Just trying to help users help themselves if possible 😀
Comment From: joseortiz3
Yes, it has been fixed. Great, thanks.
It is still an issue currently in the default anaconda, which uses 0.20.1, just fyi
Comment From: gfyoung
Absolutely, which is why we recommend upgrading if possible. Anaconda doesn't come with the most up-to-date versions generally.
Closing.