Skip to content

BUG: raise KeyError if MultiIndex.get_loc_level is asked unused label #22230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,8 @@ Missing
MultiIndex
^^^^^^^^^^

- Removed compatibility for MultiIndex pickles prior to version 0.8.0; compatibility with MultiIndex pickles from version 0.13 forward is maintained (:issue:`21654`)
-
- Removed compatibility for :class:`MultiIndex` pickles prior to version 0.8.0; compatibility with :class:`MultiIndex` pickles from version 0.13 forward is maintained (:issue:`21654`)
- :meth:`MultiIndex.get_loc_level` (and as a consequence, ``.loc`` on a :class:``MultiIndex``ed object) will now raise a ``KeyError``, rather than returning an empty ``slice``, if asked a label which is present in the ``levels`` but is unused (:issue:`22221`)
-

I/O
Expand Down
26 changes: 14 additions & 12 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2185,11 +2185,6 @@ def _maybe_to_slice(loc):

if not isinstance(key, tuple):
loc = self._get_level_indexer(key, level=0)

# _get_level_indexer returns an empty slice if the key has
# been dropped from the MultiIndex
if isinstance(loc, slice) and loc.start == loc.stop:
raise KeyError(key)
return _maybe_to_slice(loc)

keylen = len(key)
Expand Down Expand Up @@ -2443,14 +2438,21 @@ def convert_indexer(start, stop, step, indexer=indexer, labels=labels):

else:

loc = level_index.get_loc(key)
if isinstance(loc, slice):
return loc
elif level > 0 or self.lexsort_depth == 0:
return np.array(labels == loc, dtype=bool)
code = level_index.get_loc(key)

if level > 0 or self.lexsort_depth == 0:
# Desired level is not sorted
locs = np.array(labels == code, dtype=bool, copy=False)
if not locs.any():
# The label is present in self.levels[level] but unused:
raise KeyError(key)
return locs

i = labels.searchsorted(loc, side='left')
j = labels.searchsorted(loc, side='right')
i = labels.searchsorted(code, side='left')
j = labels.searchsorted(code, side='right')
if i == j:
# The label is present in self.levels[level] but unused:
raise KeyError(key)
return slice(i, j)

def get_locs(self, seq):
Expand Down
18 changes: 14 additions & 4 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ def test_apply_chunk_view():
df = DataFrame({'key': [1, 1, 1, 2, 2, 2, 3, 3, 3],
'value': compat.lrange(9)})

# return view
f = lambda x: x[:2]

result = df.groupby('key', group_keys=False).apply(f)
result = df.groupby('key', group_keys=False).apply(lambda x: x[:2])
expected = df.take([0, 1, 3, 4, 6, 7])
tm.assert_frame_equal(result, expected)

Expand Down Expand Up @@ -518,6 +515,19 @@ def test_func(x):
tm.assert_frame_equal(result2, expected2)


def test_groupby_apply_return_empty_chunk():
# GH 22221: apply filter which returns some empty groups
df = pd.DataFrame(dict(value=[0, 1], group=['filled', 'empty']))
groups = df.groupby('group')
result = groups.apply(lambda group: group[group.value != 1]['value'])
expected = pd.Series([0], name='value',
index=MultiIndex.from_product([['empty', 'filled'],
[0]],
names=['group', None]
).drop('empty'))
tm.assert_series_equal(result, expected)


def test_apply_with_mixed_types():
# gh-20949
df = pd.DataFrame({'A': 'a a b'.split(), 'B': [1, 2, 3], 'C': [4, 6, 5]})
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ def test_get_loc_level():
assert new_index is None

pytest.raises(KeyError, index.get_loc_level, (2, 2))
# GH 22221: unused label
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the issue number here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean repeat it below?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good as written

pytest.raises(KeyError, index.drop(2).get_loc_level, 2)
# Unused label on unsorted level:
pytest.raises(KeyError, index.drop(1, level=2).get_loc_level, 2, 2)

index = MultiIndex(levels=[[2000], lrange(4)], labels=[np.array(
[0, 0, 0, 0]), np.array([0, 1, 2, 3])])
Expand Down