Code Sample, a copy-pastable example if possible
E.g:
lcf.loc[i,'f'] = 5510448.7222578134
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[key] = _infer_fill_value(value)
Note:
i = 0
lcf.index = RangeIndex(start=0, stop=3895, step=1)
However the columns 'f'
doesn't exist yet.
Problem description
I don't understand why I'm getting the SettingWithCopyWarning when I am doing what it suggests. Furthermore the above code actually works, and isn't setting to a copy (since the DataFrame updates correctly).
This appears to be because I am making the column 'f'
in the above example (it's not defined before), however I thought the above was the intended way of adding new columns/rows.
Expected Output
None
Output of pd.show_versions()
Comment From: TomAugspurger
How was lcf
created? Can you make a reproducible example?
Comment From: mar-ses
I found where the problem was, it was on my end. I thought I was creating lcf
from some arrays, but instead in this example, it actually comes from a slice of a different DataFrame, i.e:
lcf = tpf[['a', 'b']]
lcf.loc[0,'f'] = 5510448.7222578134
So this becomes a copy of a slice, and when I assign the new value and try to create a new column 'f'
it gives the warning. Still, what would be the recommended replacement for the code above, to avoid the warning (not that it's so important)?
In any case, I don't mind this being closed.
Comment From: jreback
the docs include a link to here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy
you should simply .copy()