@@ -477,31 +477,24 @@ allowing you to permute the hierarchical index levels in one step:
477
477
478
478
df[:5 ].reorder_levels([1 ,0 ], axis = 0 )
479
479
480
- The need for sortedness with :class: `~pandas.MultiIndex `
481
- --------------------------------------------------------
480
+ Sorting a :class: `~pandas.MultiIndex `
481
+ -------------------------------------
482
482
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 ``.
493
485
494
486
.. ipython :: python
495
487
496
488
import random; random.shuffle(tuples)
497
489
s = pd.Series(np.random.randn(8 ), index = pd.MultiIndex.from_tuples(tuples))
498
490
s
491
+ s.sort_index()
499
492
s.sort_index(level = 0 )
500
493
s.sort_index(level = 1 )
501
494
502
495
.. _advanced.sortlevel_byname :
503
496
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
505
498
are named.
506
499
507
500
.. ipython :: python
@@ -510,24 +503,23 @@ are named.
510
503
s.sort_index(level = ' L1' )
511
504
s.sort_index(level = ' L2' )
512
505
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 :
515
508
516
509
.. ipython :: python
517
510
518
- s[' qux' ]
519
- s.sort_index(level = 1 )[' qux' ]
511
+ df.T.sort_index(level = 1 , axis = 1 )
520
512
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:
523
516
524
517
.. ipython :: python
525
518
526
- df.T.sort_index(level = 1 , axis = 1 )
519
+ s[' qux' ]
520
+ s.sort_index(level = 1 )[' qux' ]
527
521
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:
531
523
532
524
.. ipython :: python
533
525
@@ -538,18 +530,6 @@ an exception. Here is a concrete example to illustrate this:
538
530
reordered = idx[[1 , 0 , 3 , 2 ]]
539
531
reordered.lexsort_depth
540
532
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
-
553
533
554
534
Take Methods
555
535
------------
0 commit comments