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.
In this case, you should temporarily disable this warning and proceed as you are now. Using
.copy()
will mean your originaldf
will be unmoidified by changes tod
.option_context
is a context manager, meaning it can be used withwith
, and the option only applies to code within the block.Read more: pandas > Getting & Setting Options