pandas:Key is not missing but got error “Passing list-likes to .loc or [] with any missing labels is no longer supported.”

Published on:
Last updated:

This post is also available in: 日本語 (Japanese)

This is a note about dataframe key is not missing but got the following error when use loc.

KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported."

A common solution to the above error is use .reindex() because it refers to a key that does not exist with loc.

But if the key should exist and you got the above error, maybe you passed the key as an int to loc.
In other words, you passed the key as an int to loc but you think the key as str.

A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index).

reference:
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html

About
Kuniyoshi Takemoto is the founder of Amelt.net LLC, and editor of this blog(www.amelt.net).Learn more and follow me on LinkedIn.