0

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.

Zak Keirn
  • 807
  • 13
  • 22
  • It is most probably because you have previously took a slice from another DataFrame and assigned it to pandas_df. What is `print(pandas_df.is_copy)` – ayhan Feb 19 '18 at 16:06
  • Yeah that means you have taken a slice beforehand. Set `df.is_copy = None` before the assignment you have shown here. – ayhan Feb 19 '18 at 16:37
  • Since your are marking as duplicate, can you explain which part of the duplicate answer applies here? Is it just to ignore the warning? – Zak Keirn Feb 19 '18 at 16:42
  • Jeff's answer explains this situation. Rather than ignoring the warning you are saying that I am aware this is a copy and it won't modify whatever df I have sliced this from. – ayhan Feb 19 '18 at 16:46
  • Sorry, I am still not clear on how Jeff's answer applies here. He never mentioned setting df.is_copy = None. He mentions False, which he states will effectively turn off the check (ie ignore). He does mention using df.copy(). I am trying to fix the problem where it occurs. If i have something I am doing wrong prior to this, I would rather fix it there. – Zak Keirn Feb 19 '18 at 17:03
  • When you try to modify `pandas_df`, pandas throws a warning. It is because earlier in your code (the part you haven't shown here) you did something like `pandas_df = some_df[some_slice]` (second line in Jeff's answer). Jeff tells you that you can either set `pandas_df.is_copy = False` (same as None since `bool(None)` returns False) to show the slicing was intentional or be more explicit and type `pandas_df = some_df[some_slice].copy()`. – ayhan Feb 19 '18 at 17:10
  • Thank you for the follow up and detail. I understand now. – Zak Keirn Feb 19 '18 at 17:23
  • Glad to be of help. – ayhan Feb 19 '18 at 17:27

0 Answers0