Skip to content

Commit

Permalink
incorrectly raising KeyError rather than UnsortedIndexError, caught b…
Browse files Browse the repository at this point in the history
…y doc-example
  • Loading branch information
jreback committed Mar 22, 2017
1 parent 53c7e6c commit 4c880ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,10 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):

def _partial_tup_index(self, tup, side='left'):
if len(tup) > self.lexsort_depth:
raise KeyError('Key length (%d) was greater than MultiIndex'
' lexsort depth (%d)' %
(len(tup), self.lexsort_depth))
raise UnsortedIndexError(
'Key length (%d) was greater than MultiIndex'
' lexsort depth (%d)' %
(len(tup), self.lexsort_depth))

n = len(tup)
start, end = 0, len(self)
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/indexes/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2752,6 +2752,30 @@ def test_unsortedindex(self):
with assertRaises(KeyError):
df.loc(axis=0)['q', :]

def test_unsortedindex_doc_examples(self):
# http://pandas.pydata.org/pandas-docs/stable/advanced.html#sorting-a-multiindex # noqa
dfm = DataFrame({'jim': [0, 0, 1, 1],
'joe': ['x', 'x', 'z', 'y'],
'jolie': np.random.rand(4)})

dfm = dfm.set_index(['jim', 'joe'])
with tm.assert_produces_warning(PerformanceWarning):
dfm.loc[(1, 'z')]

with pytest.raises(UnsortedIndexError):
dfm.loc[(0, 'y'):(1, 'z')]

assert not dfm.index.is_lexsorted()
assert dfm.index.lexsort_depth == 1

# sort it
dfm = dfm.sort_index()
dfm.loc[(1, 'z')]
dfm.loc[(0, 'y'):(1, 'z')]

assert dfm.index.is_lexsorted()
assert dfm.index.lexsort_depth == 2

def test_tuples_with_name_string(self):
# GH 15110 and GH 14848

Expand Down

0 comments on commit 4c880ce

Please sign in to comment.