Skip to content

Commit 0f85550

Browse files
committed
added tests and fixed if statement
1 parent 2c1b85b commit 0f85550

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

pandas/core/indexing.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,10 @@ def _handle_lowerdim_multi_index_axis0(self, tup):
945945
# slices are unhashable
946946
pass
947947
except KeyError as ek:
948-
if len(tup) > self.obj.ndim:
948+
# raise KeyError if number of indexers match
949+
# else IndexingError will be raised
950+
if (len(tup) == len(self.obj.index._levels)
951+
and len(tup) > self.obj.ndim):
949952
raise ek
950953
except Exception as e1:
951954
if isinstance(tup[0], (slice, Index)):

pandas/tests/indexing/multiindex/test_loc.py

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pandas as pd
88
from pandas import DataFrame, Index, MultiIndex, Series
99
from pandas.util import testing as tm
10+
from pandas.core.indexing import IndexingError
1011

1112

1213
@pytest.fixture
@@ -130,6 +131,14 @@ def test_loc_multiindex(self):
130131
with pytest.raises(KeyError, match=r"^2L?$"):
131132
mi_int.ix[2]
132133

134+
s = Series(range(8), index=MultiIndex.from_product(
135+
[['a', 'b'], ['c', 'd'], ['e', 'f']]))
136+
137+
with pytest.raises(KeyError, match=r"^\('a', 'd', 'g'\)$"):
138+
s.loc['a', 'd', 'g']
139+
with pytest.raises(IndexingError, match='Too many indexers'):
140+
s.loc['a', 'd', 'g', 'j']
141+
133142
def test_loc_multiindex_indexer_none(self):
134143

135144
# GH6788

pandas/tests/indexing/test_loc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def test_loc_getitem_label_out_of_range(self):
122122
self.check_result('label range', 'loc', 20, 'ix', 20,
123123
typs=['ints', 'uints', 'mixed'], fails=KeyError)
124124
self.check_result('label range', 'loc', 20, 'ix', 20,
125-
typs=['labels'], fails=TypeError)
125+
typs=['labels'], fails=KeyError)
126126
self.check_result('label range', 'loc', 20, 'ix', 20, typs=['ts'],
127127
axes=0, fails=TypeError)
128128
self.check_result('label range', 'loc', 20, 'ix', 20, typs=['floats'],

0 commit comments

Comments
 (0)