我尝试了以下代码将列转换为“日期”:
df.['DATE'] = pd.to_datetime(df['DATE'])
要么
df.DATE = pd.to_datetime(df.DATE)
但出现以下错误:
/Users/xyz/anaconda3/envs/sensor/lib/python3.6/site- packages/pandas/core/indexing.py:517:SettingWithCopyWarning:试图在DataFrame的切片副本上设置一个值。尝试改用.loc [row_indexer,col_indexer] = value 请参阅文档中的警告:http : //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus- copy self.obj [item] = s
/Users/xyz/anaconda3/envs/sensor/lib/python3.6/site- packages/pandas/core/indexing.py:517:SettingWithCopyWarning:试图在DataFrame的切片副本上设置一个值。尝试改用.loc [row_indexer,col_indexer] = value
请参阅文档中的警告:http : //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus- copy self.obj [item] = s
我将代码更改为以下内容:
df.loc[:,'DATE'] = pd.to_datetime(df.loc[:,'DATE'])
但我仍然遇到相同的错误。
与此相同
for i in df.index: df.loc[i,'DATE'] = pd.to_datetime(df.loc[i,'DATE'])
您需要添加copy:
copy
df = data.loc[data.ID == 79]
至:
df = data.loc[data.ID == 79].copy()
如果df稍后再修改值,您会发现修改不会传播回原始数据(data),并且Pandas会发出警告。
df
data