Unexpected SettingWithCopyWarning:

50 Views Asked by At

I'm running code that modifies values within a certain threshold in a dataframe. I receive a warning that on the surface does not seem warranted:

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

My code already uses the a .loc[row,column] assignment, therefore I do not understand why the warning suggests that.

import pandas as pd
#pd.options.mode.chained_assignment = None #disable warning 
#pd.set_option('mode.chained_assignment','warn')#or "warn" or "raise"
u = (df 
# Group all forecasts together
     .groupby(by="forecast_id",  sort=False)
# modify only forecasts groups that have smallest value = 0 
     .filter(lambda x: x.value.min() == 0, dropna=False)
# transform values according to a function
     .value.transform( lambda x: (x+0.005).where(x == 0, x-0.005) ) 
     )
# replace the column in the dataframe with the new values except those unaffected
df.loc[pd.notnull(u), "value"] = u

The other behavior I could not explain is that as I was playing around with the warning options, once I set the Warning to None, even if I reset it back to "warn", there is no warning anymore. Note: my code is used as a function.


Edit

A description of what the code does as well an example, is provided in the link at the top; however, my focus here is understanding why why the warning is suggesting an implementation that is already implemented: Pandas - Calculate New Value Based on Cross Reference with Another Column

0

There are 0 best solutions below