-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Addressing multiindex raises TypeError if indices that are rightmost are not present #20951
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
Comments
cc @toobaz this closed by your recent MI warning? |
Unfortunately not - my PR should have only affected list(-like)s of keys, not single keys. This is rather related to #19110 and #17024 (and possibly more). Basically, since |
I'm happy to have a go fixing this behaviour - not sure how successful I'll be or when I'll have time. Is the expected behaviour that I've specified in the issue report correct? Should missing keys always raise a KeyError? |
The basic idea (real code is more complicated) is to replace something like try:
# look for tuple in index
except:
try:
# look for first element in index, second element in columns
except Exception as exc:
raise exc with something like: try:
# look for tuple in index
except Exception as exc:
try:
# look for first element in index, second element in columns
except:
raise exc
|
It's actually not so simple, because we still want to raise the current error when the index is not a |
This works now, the last statement raises
|
After doing some testing, I agree, the concrete error above seems to be solved in pandas version 1.2.2. Can the Issue be closed? |
Code Sample, a copy-pastable example if possible
Problem description
If the value is not present in the index, I believe that a KeyError should be raised consistently, so you can write code like.
Expected Output
If df.loc[tuple] does not have a match in the multiindex, a KeyError should be raised.
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.22.0
pytest: None
pip: 9.0.3
setuptools: 39.0.1
Cython: None
numpy: 1.14.2
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.7.2
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: