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

I encountered the same error with missing row index labels rather than columns.
For example, I would have a dataset of products with the following ids: ['a','b','c','d']. I store those products in a dataframe with indices ['a','b','c','d']:

df=pd.DataFrame(['product a','product b','product c', 'product d'],index=['a','b','c','d'])

Now let's assume I have an updated product index: row_indices=['b','c','d','e'] in which 'e' corresponds to a new product: 'product e'. Note that 'e' was not present in my original index ['a','b','c','d'].

If I try to pass this updated index to my df dataframe: df.loc[row_indices,:],

I'll get this nasty error message:

KeyError: "Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Index(['e'], dtype='object').

To avoid this error I need to do intersection of my updated index with the original index:

df.loc[df.index.intersection(row_indices),:]  

this is in line with recommendation of what pandas docs


It looks like Pandas has deprecated this method of indexing. According to their docs:

This behavior is deprecated and will show a warning message pointing to this section. The recommended alternative is to use .reindex()

Using the new recommended method, you can filter your columns using:

tips_filtered = tips_df.reindex(columns = filtered_columns).

NB: To reindex rows, you would use reindex(index = ...) (More information here).


Some of the columns in the list are not included in the dataframe , if you do want do that , let us try reindex

tips_filtered = tips_df.reindex(columns=filtered_columns)