Skip to content
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 operation cannot locate existing index when having single string as index for dataframe ('string',) #57750

Open
2 of 3 tasks
carlonlv opened this issue Mar 6, 2024 · 4 comments
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action

Comments

@carlonlv
Copy link

carlonlv commented Mar 6, 2024

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
temp = pd.DataFrame({'a': [1,2,3], 'b': False}, index=[('h',), ('v',), ('c',)])
print(('h',) in temp.index) ## This would print True
temp.loc[('h',), 'b'] = True ## This would result in key error

Issue Description

It seems like when having indices looking at ('a',), pandas automatically converts it into string 'a'.

KeyError: "None of [Index(['h'], dtype='object')] are in the [index]"

Expected Behavior

temp.loc[('h',)] operation should be successful

Installed Versions

INSTALLED VERSIONS ------------------ commit : bdc79c1 python : 3.11.5.final.0 python-bits : 64 OS : Linux OS-release : 6.5.0-1015-azure Version : #15~22.04.1-Ubuntu SMP Tue Feb 13 01:15:12 UTC 2024 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : C.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.2.1
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
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.1.3
IPython : 8.20.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.12.2
gcsfs : None
matplotlib : 3.8.3
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

@carlonlv carlonlv added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 6, 2024
@carlonlv
Copy link
Author

carlonlv commented Mar 6, 2024

Note that the same operation with tuples with length >= 2 works as expected.

Also in the above example, if I do temp.loc[temp.index == ('h',), 'b'] = True, it also works fine

@mbarki-mohamed
Copy link

Please have a look at the following answer on StackOverflow; (it's an old one but still relevant)
https://stackoverflow.com/questions/40186361/pandas-dataframe-with-tuple-of-strings-as-index

It will work if you pass the tuple inside a list as argument to the loc function (explanation in the link)
temp.loc[('h',), 'b'] = True # error
to
temp.loc[[('h',)], 'b'] = True # works fine

Hope this helps !

@carlonlv
Copy link
Author

carlonlv commented Mar 6, 2024

Thanks for the clarification. I was simply using the dataframe as dictionary. I guess this is one of the "gotcha" moment.

@rhshadrach
Copy link
Member

Thanks for the report. pandas uses tuples to signify values in a MultiIndex, and this is the reason why your lookup fails. One idea is to treat non-MulitIndexes differently here, allowing for lookups with tuples, whereas supplying tuples when the index is a MultiIndex would interpret them as levels. Perhaps this has some bad implications though, further investigations are welcome!

@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 7, 2024
@carlonlv carlonlv closed this as completed Mar 7, 2024
@rhshadrach rhshadrach reopened this Mar 27, 2024
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 Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

3 participants