Skip to content

Commit 4e02ef4

Browse files
committed
incorrectly raising KeyError rather than UnsortedIndexError, caught by doc-example
1 parent ae3777e commit 4e02ef4

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pandas/indexes/multi.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1794,9 +1794,10 @@ def slice_locs(self, start=None, end=None, step=None, kind=None):
17941794

17951795
def _partial_tup_index(self, tup, side='left'):
17961796
if len(tup) > self.lexsort_depth:
1797-
raise KeyError('Key length (%d) was greater than MultiIndex'
1798-
' lexsort depth (%d)' %
1799-
(len(tup), self.lexsort_depth))
1797+
raise UnsortedIndexError(
1798+
'Key length (%d) was greater than MultiIndex'
1799+
' lexsort depth (%d)' %
1800+
(len(tup), self.lexsort_depth))
18001801

18011802
n = len(tup)
18021803
start, end = 0, len(self)

pandas/tests/indexes/test_multi.py

+24
Original file line numberDiff line numberDiff line change
@@ -2752,6 +2752,30 @@ def test_unsortedindex(self):
27522752
with assertRaises(KeyError):
27532753
df.loc(axis=0)['q', :]
27542754

2755+
def test_unsortedindex_doc_examples(self):
2756+
# http://pandas.pydata.org/pandas-docs/stable/advanced.html#sorting-a-multiindex # noqa
2757+
dfm = DataFrame({'jim': [0, 0, 1, 1],
2758+
'joe': ['x', 'x', 'z', 'y'],
2759+
'jolie': np.random.rand(4)})
2760+
2761+
dfm = dfm.set_index(['jim', 'joe'])
2762+
with tm.assert_produces_warning(PerformanceWarning):
2763+
dfm.loc[(1, 'z')]
2764+
2765+
with pytest.raises(UnsortedIndexError):
2766+
dfm.loc[(0, 'y'):(1, 'z')]
2767+
2768+
assert not dfm.index.is_lexsorted()
2769+
assert dfm.index.lexsort_depth == 1
2770+
2771+
# sort it
2772+
dfm = dfm.sort_index()
2773+
dfm.loc[(1, 'z')]
2774+
dfm.loc[(0, 'y'):(1, 'z')]
2775+
2776+
assert dfm.index.is_lexsorted()
2777+
assert dfm.index.lexsort_depth == 2
2778+
27552779
def test_tuples_with_name_string(self):
27562780
# GH 15110 and GH 14848
27572781

0 commit comments

Comments
 (0)