Pandas:SettingWithCopyWarning尝试使用。

2024-04-23 16:59:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道这是一个非常普遍的错误,然而,在我的例子中,我不知道为什么会发生在我身上。在

我得到了:

 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
      df['time'] = pd.to_datetime(df['time'], unit='ms')
    : 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

代码:

^{pr2}$

你能帮我弄清楚为什么我会受到这样的警告吗?在


Tags: oftofromdataframeisvalueonslice
1条回答
网友
1楼 · 发布于 2024-04-23 16:59:30

这是一个有据可查的警告。 解决

df.loc[:,'time'] = pd.to_datetime(df['time'], unit='ms')

发生这种情况是因为df['time']是一个视图,而不是要编辑的实际数据。 请检查代码中的其他地方,因为每当您分配类似的列时,都会弹出此警告

^{pr2}$

正确的是

   df.loc[:, column_name]=something

相关问题 更多 >