Search code examples
pythonpandaschained-assignment

Can't get around Pandas Series SettingWithCopyWarning


I'd like to fetch a Series and make changes to it, which I'd like reflected in the DataFrame later on. However I can't understand how to do it without the SettingWithCopyWarning. Is this a false positive or am I doing something wrong?

df = pd.DataFrame([[1,2,3],[4,5,6]], columns=list('abc'))
df['d'] = df['a'].diff()
d = df.loc[:, 'd']
d.loc[d>0] *= 3

I've read the docs (and yes, I did read this question before asking but it only deals with DataFrames and not with Series), but isn't able to work out how to fix this. I would prefer not to disable the warning, as I have code where I don't want to make this type of mistake inadvertently.


Solution

  • I'd like to fetch a Series and make changes to it, which I'd like reflected in the DataFrame later on.

    In this case, you should temporarily disable this warning and proceed as you are now. Using .copy() will mean your original df will be unmoidified by changes to d.

    with pd.option_context('mode.chained_assignment', None):
        df = pd.DataFrame([[1,2,3],[4,5,6]], columns=list('abc'))
        df['d'] = df['a'].diff()
        d = df.loc[:, 'd']
        d.loc[d>0] *= 3
    
    # Code you run outside of `with` will maintain your original setting:
    # pd.get_option('chained_assignment')
    

    option_context is a context manager, meaning it can be used with with, and the option only applies to code within the block.

    Read more: pandas > Getting & Setting Options