# Buggy code
def sort_index(
    self,
    axis=0,
    level=None,
    ascending=True,
    inplace=False,
    kind="quicksort",
    na_position="last",
    sort_remaining=True,
):
    inplace = validate_bool_kwarg(inplace, "inplace")
    axis = self._get_axis_number(axis)
    index = self.index
    if level is not None:
        new_index, indexer = index.sortlevel(
            level, ascending=ascending, sort_remaining=sort_remaining
        )
    elif isinstance(index, MultiIndex):
        from pandas.core.sorting import lexsort_indexer

        indexer = lexsort_indexer(index.labels, orders=ascending)
    else:
        from pandas.core.sorting import nargsort

        indexer = nargsort(
            index, kind=kind, ascending=ascending, na_position=na_position
        )

    indexer = _ensure_platform_int(indexer)
    new_index = index.take(indexer)

    new_values = self._values.take(indexer)
    result = self._constructor(new_values, index=new_index)

    if inplace:
        self._update_inplace(result)
    else:
        return result.__finalize__(self)


# Traceback (KeyError)
df2.sort_index().loc['b':'d']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)

# a lot of lines omitted here.

/Users/yimengzh/miniconda/envs/cafferc3/lib/python2.7/site-packages/pandas/indexes/multi.py in _partial_tup_index(self, tup, side)
1488             raise KeyError('Key length (%d) was greater than MultiIndex'
1489                            ' lexsort depth (%d)' %
-> 1490                            (len(tup), self.lexsort_depth))
1491
1492         n = len(tup)

KeyError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'

# Fixed code
def sort_index(
    self,
    axis=0,
    level=None,
    ascending=True,
    inplace=False,
    kind="quicksort",
    na_position="last",
    sort_remaining=True,
):
    # TODO: this can be combined with DataFrame.sort_index impl as
    # almost identical
    inplace = validate_bool_kwarg(inplace, "inplace")
    axis = self._get_axis_number(axis)
    index = self.index

    if level:
        new_index, indexer = index.sortlevel(
            level, ascending=ascending, sort_remaining=sort_remaining
        )
    elif isinstance(index, MultiIndex):
        from pandas.core.sorting import lexsort_indexer

        labels = index._sort_levels_monotonic()
        indexer = lexsort_indexer(labels.labels, orders=ascending)
    else:
        from pandas.core.sorting import nargsort

        # Check monotonic-ness before sort an index
        # GH11080
        if (ascending and index.is_monotonic_increasing) or (
            not ascending and index.is_monotonic_decreasing
        ):
            if inplace:
                return
            else:
                return self.copy()

        indexer = nargsort(
            index, kind=kind, ascending=ascending, na_position=na_position
        )

    indexer = _ensure_platform_int(indexer)
    new_index = index.take(indexer)

    new_values = self._values.take(indexer)
    result = self._constructor(new_values, index=new_index)

    if inplace:
        self._update_inplace(result)
    else:
        return result.__finalize__(self)