Pandas slicing FutureWarning with 0.21.0
TL;DR: There is likely a typo or spelling error in the column header names.
This is a change introduced in v0.21.1
, and has been explained in the docs at length -
Previously, selecting with a list of labels, where one or more labelswere missing would always succeed, returning
NaN
for missing labels.This will now show aFutureWarning
. In the future this will raise aKeyError
(GH15747). This warning will trigger on aDataFrame
or aSeries
for using.loc[]
or[[]]
when passing a list-of-labels with atleast 1 missing label.
For example,
df A B C0 7.0 NaN 81 3.0 3.0 52 8.0 1.0 73 NaN 0.0 34 8.0 2.0 7
Try some kind of slicing as you're doing -
df.loc[df.A.gt(6), ['A', 'C']] A C0 7.0 82 8.0 74 8.0 7
No problem. Now, try replacing C
with a non-existent column label -
df.loc[df.A.gt(6), ['A', 'D']]FutureWarning: Passing list-likes to .loc or [] with any missing label will raiseKeyError in the future, you can use .reindex() as an alternative. A D0 7.0 NaN2 8.0 NaN4 8.0 NaN
So, in your case, the error is because of the column labels you pass to loc
. Take another look at them.
This error also occurs with .append
call when the list contains new columns. To avoid this
Use:
df=df.append(pd.Series({'A':i,'M':j}), ignore_index=True)
Instead of,
df=df.append([{'A':i,'M':j}], ignore_index=True)
Full error message:
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:1472: FutureWarning: Passing list-likes to .loc or with any missing label will raise KeyError in the future, you can use .reindex() as an alternative.