BUG: Index and MultiIndex KeyError
cases and discussion
#39775
Labels
API Design
Bug
Error Reporting
Incorrect or improved errors from pandas
Indexing
Related to indexing on series/frames, not to indexes themselves
MultiIndex
Since the introduction of
KeyError
for missing keys in anindex
there have been quite a few use cases from different issues. I will try and link some of the issues if I see them.My view is that
KeyErrors
forIndex
is fine, butMultiIndexes
should be treated differently: you cannot always raise aKeyError
for a single keys in aMultiIndex
slice since aMultiIndex
cannot always be reindexed.Index
Code generator
This seems to be pretty consistent. The only inconsistency is perhaps highlighted in red, and a minor niggle for dynamic coding might be the different return types in the case of non-unique indexes.
Obviously the solution to dealing with any case where you need to index by pre-defined levels that may have been filtered is to
reindex
with your pre-defined keys. Any this is quite easy to do in RAM.MultiIndex
MultiIndexing is different. You cannot always reindex for one of two reasons:
For example, consider the MultiIndex levels: (a,b), (x,y,z). There are a maximum of 6 index tuples but practically one will work with indexes of much less than the maximum combinations (Since the combinations scale exponentially with the number of levels). Your MultiIndex is thus [(a,x), (a,z), (b,x), (b,y)].
I think you need to be able to index MultiIndexes with keys that are missing. As a rule I would suggest that slices which are an iterable do not yield KeyErrors. Here is a summary of some of the observances below for current behaviour:
Code generator
The text was updated successfully, but these errors were encountered: