Pandas: SettingWithCopyWarning:

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โ–บ17: SettingWithCopyWarning: value is trying to set a slice from a DataFrame on the copy. Try using .loc [row_indexer, col_indexer] = instead

See reservations 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 thing with that

for i in df.index:
    df.loc[i,'DATE'] =  pd.to_datetime(df.loc[i,'DATE'])
+4
source share
2 answers

You need to add copy:

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

at

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

df , , (data) Pandas .

+3

, . - - :

df = other.loc[something]

. .loc DataFrame:

other.loc[something, 'DATE'] = whatever
+2

Source: https://habr.com/ru/post/1681714/


All Articles