Encountering SettingWithCopyWarning when replacing values in dataframe

Screen Link:

My Code:

Two tasks:
- Turn 0 PS rows into NaN's
- turn > 1000 PS rows into NaN's
autos.loc[autos['power_ps'] == 0,'power_ps'] = np.NaN
autos.loc[autos['power_ps'] > 1000,'power_ps'] = np.NaN

What I expected to happen:
I am trying to turn values that are likely input errors into NaN’s.
Everything is working fine, and after checking the output thoroughly it seems to me that everything went as expected, however, i’m getting this error:

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py:966: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

I’ve googled it and i get that its not an error per se yet is not to be taken lightly.
What puts me off is that the syntax it suggests, is basically what i’m using, so i’m not sure what causes the warning and how i can make sure things are correct.
Is there a way to achieve the above without getting this warning?
Is there anything i am missing?

Thanks for the feedback!

1 Like

Hello @luke.niklaus,

This would help


Luke, could you post your updated code? The article only confuses me more… I’m not able to get rid of the warning no matter where I put the .copy()

Hi @ananth.ch,

Here is another article, that may help you with this:


1 Like