Open carlonlv opened 8 months ago
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
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 !
Thanks for the clarification. I was simply using the dataframe as dictionary. I guess this is one of the "gotcha" moment.
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!
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] 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
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