Reputation: 25
Could some one explain to me why use this code:
df[["normalized-losses"]].replace(np.nan, avg_norm_loss, inplace=True)
Give-me this error: "/home/jupyterlab/conda/envs/python/lib/python3.6/site-packages/pandas/core/frame.py:4389: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame"
While this code works perfectly
df["normalized-losses"].replace(np.nan, avg_norm_loss, inplace=True)
I have a theory that's because the method replace have to use paramaters with these types: str, regex, list, dict, Series, int, float, or None, so when I tried to use the first code I have a dataframe type, am I right or wrong?
Upvotes: 0
Views: 88
Reputation: 260580
When you do the following df[['column']]
, pandas creates a new object in memory. If you assign a variable using new_df = df[['column']]
, then the object is not destroyed. However, when you apply an 'inplace' method to a copy in memory, pandas creates this object for nothing as it will be destroyed. Thus the SettingWithCopyWarning
flag was created to warn users that they are performing a useless operation.
In contrast, using df['column']
, this returns an actual Series that is present in the DataFrame and that can be modified in place.
Upvotes: 1