@@ -477,31 +477,24 @@ allowing you to permute the hierarchical index levels in one step:
477477
478478 df[:5 ].reorder_levels([1 ,0 ], axis = 0 )
479479
480- The need for sortedness with :class: `~pandas.MultiIndex `
481- --------------------------------------------------------
480+ Sorting a :class: `~pandas.MultiIndex `
481+ -------------------------------------
482482
483- **Caveat emptor **: the present implementation of ``MultiIndex `` requires that
484- the labels be sorted for some of the slicing / indexing routines to work
485- correctly. You can think about breaking the axis into unique groups, where at
486- the hierarchical level of interest, each distinct group shares a label, but no
487- two have the same label. However, the ``MultiIndex `` does not enforce this:
488- **you are responsible for ensuring that things are properly sorted **. There is
489- an important new method ``sort_index `` to sort an axis within a ``MultiIndex ``
490- so that its labels are grouped and sorted by the original ordering of the
491- associated factor at that level. Note that this does not necessarily mean the
492- labels will be sorted lexicographically!
483+ For MultiIndex-ed objects to be indexed & sliced efficiently, they need
484+ to be sorted. As with any index, you can use ``sort_index ``.
493485
494486.. ipython :: python
495487
496488 import random; random.shuffle(tuples)
497489 s = pd.Series(np.random.randn(8 ), index = pd.MultiIndex.from_tuples(tuples))
498490 s
491+ s.sort_index()
499492 s.sort_index(level = 0 )
500493 s.sort_index(level = 1 )
501494
502495 .. _advanced.sortlevel_byname :
503496
504- Note, you may also pass a level name to ``sort_index `` if the MultiIndex levels
497+ You may also pass a level name to ``sort_index `` if the MultiIndex levels
505498are named.
506499
507500.. ipython :: python
@@ -510,24 +503,23 @@ are named.
510503 s.sort_index(level = ' L1' )
511504 s.sort_index(level = ' L2' )
512505
513- Some indexing will work even if the data are not sorted, but will be rather
514- inefficient and will also return a copy of the data rather than a view :
506+ On higher dimensional objects, you can sort any of the other axes by level if
507+ they have a MultiIndex :
515508
516509.. ipython :: python
517510
518- s[' qux' ]
519- s.sort_index(level = 1 )[' qux' ]
511+ df.T.sort_index(level = 1 , axis = 1 )
520512
521- On higher dimensional objects, you can sort any of the other axes by level if
522- they have a MultiIndex:
513+ Some indexing will work even if the data are not sorted, but will be rather
514+ inefficient (and show a ``PerformanceWarning ``). It will also
515+ return a copy of the data rather than a view:
523516
524517.. ipython :: python
525518
526- df.T.sort_index(level = 1 , axis = 1 )
519+ s[' qux' ]
520+ s.sort_index(level = 1 )[' qux' ]
527521
528- The ``MultiIndex `` object has code to **explicitly check the sort depth **. Thus,
529- if you try to index at a depth at which the index is not sorted, it will raise
530- an exception. Here is a concrete example to illustrate this:
522+ The ``lexsort_depth `` property returns the sort depth:
531523
532524.. ipython :: python
533525
@@ -538,18 +530,6 @@ an exception. Here is a concrete example to illustrate this:
538530 reordered = idx[[1 , 0 , 3 , 2 ]]
539531 reordered.lexsort_depth
540532
541- s = pd.Series(np.random.randn(4 ), index = reordered)
542- s.ix[' a' :' a' ]
543-
544- However:
545-
546- ::
547-
548- >>> s.ix[('a', 'b'):('b', 'a')]
549- Traceback (most recent call last)
550- ...
551- KeyError: Key length (3) was greater than MultiIndex lexsort depth (2)
552-
553533
554534 Take Methods
555535------------
0 commit comments