【发布时间】:2021-05-25 22:23:45
【问题描述】:
我的目的是将date列从日期框架df中的对象类型转换为日期时间类型,但在运行程序时遭受了很多查看和复制警告。
我从链接中找到了一些有用的信息:https://stackoverflow.com/a/25254087/3849539
并测试了以下三种解决方案,它们都按预期工作,但警告消息不同。任何人都可以帮助解释他们的差异并指出为什么仍然警告消息返回视图而不是副本?谢谢。
解决方案 1:df['date'] = df['date'].astype('datetime64')
test.py:85: SettingWithCopyWarning: 试图在 从 DataFrame 复制切片。尝试使用 .loc[row_indexer,col_indexer] = 值 而是
请参阅文档中的注意事项: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy df['date'] = df['date'].astype('datetime64')
解决方案 2:df['date'] = pd.to_datetime(df['date'])
~/report/lib/python3.8/site-packages/pandas/core/frame.py:3188: SettingWithCopyWarning:试图在一个副本上设置一个值 从 DataFrame 切片。尝试使用 .loc[row_indexer,col_indexer] = 值 而是
请参阅文档中的注意事项: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy 自我[k1] = 价值[k2] test.py:85: SettingWithCopyWarning: 值是 试图在数据帧的切片副本上设置。尝试使用 .loc[row_indexer,col_indexer] = 值 而是
请参阅文档中的注意事项: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
解决方案 3:df.loc[:, 'date'] = pd.to_datetime(df.loc[:, 'date'])
~/report/lib/python3.8/site-packages/pandas/core/indexing.py:1676: SettingWithCopyWarning:试图在一个副本上设置一个值 从 DataFrame 切片。尝试使用 .loc[row_indexer,col_indexer] = value 而是
请参阅文档中的注意事项: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self._setitem_single_column(ilocs[0], value, pi)
【问题讨论】:
标签: python pandas dataframe warnings slice