0

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

gciriani
  • 611
  • 2
  • 7
  • 19
  • Probably because you have chained `lambda`s that get passed along the chain. This code is close to illegible to me – roganjosh Apr 06 '19 at 15:55
  • `# transform values according to a function` is not helpful as a comment since it doesn't detail anything other than what the following line actually does. You'd hope people reading the code could understand code, so this is actually obfuscation. – roganjosh Apr 06 '19 at 15:58
  • @roganjosh, I've added a description of the code. – gciriani Apr 06 '19 at 18:55

0 Answers0