-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: loc-indexing without MultiIndex
has inconsistent behaviour if subkeys are tuples
#53535
Comments
I agree with the conclusion here. I think this would require a deprecation of the current behavior of |
I can add a deprecation warning in For posterity, I'll record some of my investigation here.
So we first attempt to pull out a lower-dimensional object. If the key is The indexer validates (it has the correct shape). It is not a "multi-take" opportunity because "a" is not list like. So we go to
This assertion fires because the first thing we do is Conversely, if the key is |
Aside, this means that it is actually impossible, with the import pandas as pd
s = pd.Series([1], index=[(1, 2)])
s.loc[(1, 2)] # IndexingError: too many indexers
s.loc[((1, 2), )] # explicitly try and say "this is a label for the rows" => KeyError (because the tuple is treated as list-like)
|
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
Issue Description
When a dataframe is
loc
-indexed with a tuple in a the first (row) slot and a scalar key in the column slot, we get an assertionerror, which is inconsistent with the behaviour if passing a non-scalar in the column slot.Expected Behavior
Tuples are usually used in
loc/iloc
indexing to indicate grouping of indices (between rows/columns) and levels in a multiindex.iloc
-indexing raises anIndexError
if any sub-indexer is itself a tuple (which is reasonable).How should
loc
-indexing behave here? I think it is not as easy as theiloc
case because a tuple can legitimately be a key for aIndex
(not a multiindex). How about, if the index is not a multiindex, then a tuple is treated as a scalar key, and any failing lookup raisesKeyError
. Rather than a tuple being treated like any other sequence.So, concretely, in the above example all three approaches show raise an indexing error, since the label
(1, 2)
is not in the row index.In any case, the scalar-ness (or not) of the column key should not influence the eventual outcome in terms of whether an entry is found.
Installed Versions
INSTALLED VERSIONS
commit : 37ea63d
python : 3.11.3.final.0
python-bits : 64
OS : Linux
OS-release : 5.19.0-43-generic
Version : #44~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon May 22 13:39:36 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 2.0.1
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.7.2
pip : 23.1.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.13.2
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: