Skip to content

Commit

Permalink
added tests and fixed if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanreh99 committed Apr 20, 2019
1 parent 2c1b85b commit 0f85550
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,10 @@ def _handle_lowerdim_multi_index_axis0(self, tup):
# slices are unhashable
pass
except KeyError as ek:
if len(tup) > self.obj.ndim:
# raise KeyError if number of indexers match
# else IndexingError will be raised
if (len(tup) == len(self.obj.index._levels)
and len(tup) > self.obj.ndim):
raise ek
except Exception as e1:
if isinstance(tup[0], (slice, Index)):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series
from pandas.util import testing as tm
from pandas.core.indexing import IndexingError


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

s = Series(range(8), index=MultiIndex.from_product(
[['a', 'b'], ['c', 'd'], ['e', 'f']]))

with pytest.raises(KeyError, match=r"^\('a', 'd', 'g'\)$"):
s.loc['a', 'd', 'g']
with pytest.raises(IndexingError, match='Too many indexers'):
s.loc['a', 'd', 'g', 'j']

def test_loc_multiindex_indexer_none(self):

# GH6788
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_loc_getitem_label_out_of_range(self):
self.check_result('label range', 'loc', 20, 'ix', 20,
typs=['ints', 'uints', 'mixed'], fails=KeyError)
self.check_result('label range', 'loc', 20, 'ix', 20,
typs=['labels'], fails=TypeError)
typs=['labels'], fails=KeyError)
self.check_result('label range', 'loc', 20, 'ix', 20, typs=['ts'],
axes=0, fails=TypeError)
self.check_result('label range', 'loc', 20, 'ix', 20, typs=['floats'],
Expand Down

0 comments on commit 0f85550

Please sign in to comment.