Skip to content

Commit bc61010

Browse files
committed
DOC: mention that stack/unstack implicitly sort
1 parent 0d3229d commit bc61010

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

doc/source/reshaping.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ unstacks the **last level**:
151151
stacked.unstack(1)
152152
stacked.unstack(0)
153153
154+
Notice that the ``stack`` and ``unstack`` methods implicitly sort the index
155+
levels involved. Hence a call to ``stack`` and then ``unstack``, or viceversa,
156+
will result in a **sorted** copy of the original DataFrame or Series:
157+
158+
.. ipython:: python
159+
160+
index = MultiIndex.from_product([[2,1], ['a', 'b']])
161+
df = DataFrame(randn(4), index=index, columns=['A'])
162+
df
163+
all(df.unstack().stack() == df.sort())
164+
165+
while the above code will raise a ``TypeError`` if the call to ``sort`` is
166+
removed.
167+
154168
.. _reshaping.unstack_by_name:
155169

156170
If the indexes have names, you can use the level names instead of specifying

0 commit comments

Comments
 (0)