I am trying to simply clip specific columns in a pandas dataframe. Everything I have tried so far generates the SettingWithCopyWarning. In below: action_cols is a list of column names that is a small subset of all the columns in the data frame.
pandas_df[action_cols] = pandas_df[action_cols].clip(lower=0, upper=clip_upper)
Above generates a SettingWithCopyWarning. From reading pandas docs and other posts, I then tried the following hoping to remove the warning:
pandas_df.loc[:,action_cols] = pandas_df.loc[:,action_cols].clip(lower=0, upper=clip_upper)
The above code still generates the warning. Please explain why the warning is generated in these cases and also provide a way to do this without generating the warning. Note that I want to retain all the original columns, otherwise I could just assign the result to a new dataframe and get no warning.