Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting values on a copy of a slice from a DataFrame [duplicate]

Tags:

python

pandas

I have a small dataframe, say this one :

    Mass32      Mass44  
12  0.576703    0.496159
13  0.576658    0.495832
14  0.576703    0.495398    
15  0.576587    0.494786
16  0.576616    0.494473
...

I would like to have a rolling mean of column Mass32, so I do this:

x['Mass32s'] = pandas.rolling_mean(x.Mass32, 5).shift(-2)

It works as in I have a new column named Mass32s which contains what I expect it to contain but I also get the warning message:

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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

I'm wondering if there's a better way to do it, notably to avoid getting this warning message.

like image 859
vincent Avatar asked Jul 17 '15 03:07

vincent


People also ask

What is setting with copy warning?

Warnings should never be ignored. If you have ever done data analysis or manipulation with Pandas, it is highly likely that you encounter the SettingWithCopy warning at least once. This warning occurs when we try to do an assignment using chained indexing because chained indexing has inherently unpredictable results.

How do you copy values in a data frame?

If you want to make an explicit copy of a pandas object, try new_frame = frame. copy() .

Does LOC return a view or copy?

loc[mask] returns a new DataFrame with a copy of the data from df .


1 Answers

This warning comes because your dataframe x is a copy of a slice. This is not easy to know why, but it has something to do with how you have come to the current state of it.

You can either create a proper dataframe out of x by doing

x = x.copy()

This will remove the warning, but it is not the proper way

You should be using the DataFrame.loc method, as the warning suggests, like this:

x.loc[:,'Mass32s'] = pandas.rolling_mean(x.Mass32, 5).shift(-2)
like image 166
firelynx Avatar answered Sep 28 '22 06:09

firelynx