0
def nan_to_zeros(df, col):
    new_col = f"nanreplace{col}"
    df[new_col] = df[col].fillna('~')
    return df

When running this function I get the SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. warning and from what I can gather I am not using a chained assignment and adding inplace=True does nothing to silence the warning.

Any direction appreciated. Thanks!

Justin Benfit
  • 423
  • 3
  • 11
  • 1
    Your df reference is probably to a view into a larger dataframe. As long as you're not throwing away that reference you don't have to worry. – CJR Feb 14 '22 at 21:27
  • 4
    Note if `df` was ever subset unsafely at _any point before_ being passed to this function you will get a `SettingWithCopyWarning` even if one is not created within the function itself. You want to look for lines of code like: `new_df = df[cols]` or `new_df = df[mask]` or in this case even `nan_to_zeros(df[mask], 'col_name')` has the potential to raise this warning. – Henry Ecker Feb 14 '22 at 21:44

0 Answers0