Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
API: Use different exception type when MultiIndex indexing fails due to index not being lexsorted #11897
Comments
|
pls show a complete example uptop |
|
Here is an example: mi = pd.MultiIndex.from_tuples([('z','a'),('x','a'),('y','b'),('x','b'),('y','a'),('z','b')], names=['one','two'])
df = pd.DataFrame([[i,10*i] for i in range(6)], index=mi, columns=['one','two'])
print(df.loc(axis=0)['z',:])The last line fails with: Now consider where we do the sorting: df.sort_index(inplace=True)
print(df.loc(axis=0)['z',:])
print(df.loc(axis=0)['q',:])The second line works because things are now sorted, but the last line fails with: So I'd like to be able to surround all of the above code with ways of detecting the 2 different errors, which will make debugging easier. |
|
I like this idea. To preserve backwards compatibility, we might make it a KeyError subclass. Note that there are also some cases where indexing non-multiindexes can raise a KeyError because the index is unsourced (notably, when slicing). |
jreback
added API Design Error Reporting
labels
Dec 26, 2015
jreback
added this to the
Next Major Release
milestone
Dec 26, 2015
jreback
added Difficulty Novice Effort Low
labels
Dec 26, 2015
|
would for sure have to be a |
|
@jreback So should I create the |
jreback
modified the milestone: 0.18.1, Next Major Release
Mar 17, 2016
jreback
referenced
this issue
Mar 17, 2016
Open
.loc sometimes raises KeyError without an error message when called on an unsorted MultiIndex DataFrame #12660
|
@Dr-Irv interested in working on this? |
|
Yes, but it might be a while, as I've got some other pressing issues. |
jreback
referenced
this issue
Apr 4, 2016
Closed
BUG: loc raises inconsistent error on unsorted MultiIndex #12790
jreback
modified the milestone: 0.18.1, 0.18.2
Apr 26, 2016
jreback
referenced
this issue
May 5, 2016
Open
Slicing on DatetimeIndex throws KeyError: [int] not found #13090
jorisvandenbossche
modified the milestone: 0.20.0, 0.19.0
Aug 21, 2016
Dr-Irv
added a commit
to Dr-Irv/pandas
that referenced
this issue
Nov 29, 2016
|
|
Dr-Irv |
5f7eee1
|
Dr-Irv
added a commit
to Dr-Irv/pandas
that referenced
this issue
Dec 6, 2016
|
|
Dr-Irv |
f33093b
|
Dr-Irv
added a commit
to Dr-Irv/pandas
that referenced
this issue
Dec 7, 2016
|
|
Dr-Irv |
0947982
|
Dr-Irv
added a commit
to Dr-Irv/pandas
that referenced
this issue
Dec 9, 2016
|
|
Dr-Irv |
76b6434
|
Dr-Irv commentedDec 24, 2015
I'd like to be able to separately trap errors in indexing that are due to the lack of the
MultiIndexbeing lexsorted. Right now, aKeyErroris raised, and the only way to tell if the error was due to the lack of a lexsort versus a missing key is to look at the text of the message.So I'd like to avoid code like this:
I'd rather write something like:
So could the designers accept a change where a different error is raised in case the index is not lexsorted? If so, I'll implement it.