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()

[paste the output of ``pd.show_versions()`` here below this line] INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Linux OS-release: 2.6.32-696.10.2.el6.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_GB.UTF-8 LOCALE: en_GB.UTF-8 pandas: 0.20.1 pytest: 3.0.7 pip: 9.0.1 setuptools: 27.2.0 Cython: 0.25.2 numpy: 1.11.3 scipy: 0.19.0 xarray: None IPython: 5.3.0 sphinx: 1.5.6 patsy: 0.4.1 dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: 1.2.1 tables: 3.3.0 numexpr: 2.6.2 feather: None matplotlib: 2.0.2 openpyxl: 2.4.7 xlrd: 1.0.0 xlwt: 1.2.0 xlsxwriter: 0.9.6 lxml: 3.7.3 bs4: 4.6.0 html5lib: 0.999 sqlalchemy: 1.1.9 pymysql: None psycopg2: None jinja2: 2.9.6 s3fs: None pandas_gbq: None pandas_datareader: None

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()