Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: SettingWithCopyWarning: [duplicate]

Tags:

python

pandas

I tried the following code to convert a column to "date":

df.['DATE'] =  pd.to_datetime(df['DATE'])

or

df.DATE =  pd.to_datetime(df.DATE)

but I get the following error:

/Users/xyz/anaconda3/envs/sensor/lib/python3.6/site-packages/pandas/core/indexing.py:517: 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[item] = s

I changed the code to the following:

df.loc[:,'DATE'] =  pd.to_datetime(df.loc[:,'DATE'])

but I still get the same error.

same with this

for i in df.index:
    df.loc[i,'DATE'] =  pd.to_datetime(df.loc[i,'DATE'])
like image 872
mallet Avatar asked Jul 18 '17 14:07

mallet


People also ask

What is SettingWithCopyWarning?

A SettingWithCopyWarning warns the user of a potential bug and should never be ignored even if the program runs as expected. The warning arises when a line of code both gets an item and sets an item. Pandas does not assure whether the get item returns a view or a copy of the dataframe.

How do I get rid of copy warning settings?

The solution is simple: combine the chained operations into a single operation using loc so that pandas can ensure the original DataFrame is set. Pandas will always ensure that unchained set operations, like the below, work. This is what the warning suggests we do, and it works perfectly in this case.

How do I copy a data frame?

Pandas DataFrame copy() Method The copy() method returns a copy of the DataFrame. By default, the copy is a "deep copy" meaning that any changes made in the original DataFrame will NOT be reflected in the copy.


2 Answers

You need add copy:

df = data.loc[data.ID == 79]

to:

df = data.loc[data.ID == 79].copy()

If you modify values in df later you will find that the modifications do not propagate back to the original data (data), and that Pandas does warning.

like image 187
jezrael Avatar answered Sep 22 '22 02:09

jezrael


The problem is in the code you have not shown us. Somewhere, you have done something like this:

df = other.loc[something]

That is the root cause of this error message. You need to assign using .loc or similar directly into the original DataFrame:

other.loc[something, 'DATE'] = whatever
like image 20
John Zwinck Avatar answered Sep 23 '22 02:09

John Zwinck