Open wence- opened 1 year ago
The same kind of thing occurs for slices. If the sliced dataframe doesn't intersect with the pieces taken out by the other indexers (if they are not slices), then a keyerror is raised if the remaining indexers appear to the right of the slice, but not if they appear to the left.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] I have confirmed this bug exists on the latest version of pandas.
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
If missing scalar or list-like keys appear to the right of an empty list-like in an indexing tuple to a multi-index lookup, no
KeyError
is raised (and instead an empty dataframe is returned). In contrast, if the missing key is to the left of the empty list, then we do obtain aKeyError
.This appears to be a consequence of looking up from left to right with early exit for empty list likes.
Expected Behavior
It would be nice if the lookup consistently either raised a
KeyError
, or returned an empty dataframe.An empty dataframe would be perhaps more performant (if one collected the indexers before applying them), but a KeyError seems to go by the principle of least surprise.
cc @attack68 who I think has had some opinions about whether multiindexing should raise keyerrors as often as normal indexing in the past.
Installed Versions