Skip to content

BUG: MultiIndex KeyError message unreliable #46549

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

Closed
3 tasks done
kwsp opened this issue Mar 28, 2022 · 1 comment
Closed
3 tasks done

BUG: MultiIndex KeyError message unreliable #46549

kwsp opened this issue Mar 28, 2022 · 1 comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@kwsp
Copy link

kwsp commented Mar 28, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# Expected
df = pd.DataFrame({("x", "x"): [1]})
df[("x", "y")] # throws KeyError: ('x', 'y')

# Bug
df = pd.DataFrame({("x", "x"): [1], ("y", "y"): [1]})
df[("x", "y")]  # throws KeyError: 6

Issue Description

Normally when indexing with a key that doesn't exist, Pandas throws a KeyError with the value of the key printed in the exception string.

When using MultiIndex as the column key and trying to index with a non-existent key, sometimes the KeyError throws the right name, sometimes it does not, which makes the error message arcane and difficult to debug.

Expected Behavior

When indexing with a key that doesn't exist, Pandas should always print the value of the key when throwing a KeyError. I.e. both examples above should throw KeyError: ('x', 'y')

Installed Versions

macOS

INSTALLED VERSIONS

commit : 06d2301
python : 3.10.1.final.0
python-bits : 64
OS : Darwin
OS-release : 21.1.0
Version : Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.1
numpy : 1.21.5
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 59.5.0
Cython : 0.29.28
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

Windows

INSTALLED VERSIONS

commit : 06d2301
python : 3.10.0.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19042
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 5, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.4.1
numpy : 1.22.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.0.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.7.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@kwsp kwsp added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 28, 2022
@kwsp kwsp changed the title BUG: MultiIndex KeyError unreliable BUG: MultiIndex KeyError message unreliable Mar 28, 2022
@phofl
Copy link
Member

phofl commented Apr 8, 2022

Duplicate of #44252

@phofl phofl marked this as a duplicate of #44252 Apr 8, 2022
@phofl phofl closed this as completed Apr 8, 2022
@phofl phofl added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

2 participants