Skip to content

Bug in loc did not raise KeyError when missing combination with slice(None) was given #37764

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 9 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ Indexing
- Bug in :meth:`Index.where` incorrectly casting numeric values to strings (:issue:`37591`)
- Bug in :meth:`Series.loc` and :meth:`DataFrame.loc` raises when numeric label was given for object :class:`Index` although label was in :class:`Index` (:issue:`26491`)
- Bug in :meth:`DataFrame.loc` returned requested key plus missing values when ``loc`` was applied to single level from :class:`MultiIndex` (:issue:`27104`)
- Bug in :meth:`DataFrame.loc` did not raise ``KeyError`` when missing combination was given with ``slice(None)`` for remaining levels (:issue:`19556`)

Missing
^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,8 @@ def _getitem_axis(self, key, axis: int):
# nested tuple slicing
if is_nested_tuple(key, labels):
locs = labels.get_locs(key)
if not len(locs):
raise KeyError(key)
Copy link
Member

Choose a reason for hiding this comment

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

if this is going to raise (not 100% sure, but leaning towards yes), i think it should happen in MultiIndex.get_locs

Copy link
Member Author

Choose a reason for hiding this comment

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

Could not find any information in the docs, if get_locs should raise when the combination of both keys is not found in MultiIndex. Would prefer that too, but did not want to break anything, hence my question above. Will try to introduce this there

Copy link
Member Author

Choose a reason for hiding this comment

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

We are raising in get_locs now. This should be much more stable

indexer = [slice(None)] * self.ndim
indexer[axis] = locs
return self.obj.iloc[tuple(indexer)]
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,20 @@ def test_missing_key_raises_keyerror2(self):
with pytest.raises(KeyError, match=r"\(0, 3\)"):
ser.loc[0, 3]

def test_missing_key_combination(self):
# GH: 19556
mi = MultiIndex.from_arrays(
[
np.array(["a", "a", "b", "b"]),
np.array(["1", "2", "2", "3"]),
np.array(["c", "d", "c", "d"]),
],
names=["one", "two", "three"],
)
df = DataFrame(np.random.rand(4, 3), index=mi)
with pytest.raises(KeyError, match=r"\('b', '1', slice\(None, None, None\)\)"):
df.loc[("b", "1", slice(None)), :]


def test_getitem_loc_commutability(multiindex_year_month_day_dataframe_random_data):
df = multiindex_year_month_day_dataframe_random_data
Expand Down