一个DataFrame赋值的诡异报错 A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc

DataFrame赋值时报错

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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self.obj[key] = _infer_fill_value(value)
D:\ProgramData\Anaconda3\envs\my3.6\lib\site-packages\pandas\core\indexing.py:1743: 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: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  isetter(ilocs[0], value)

一个DataFrame赋值的诡异报错 A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc_第1张图片在以前的版本上我们对dataFrame重新创建一列,并赋值可以直接:

arriveData.loc["dayInMonth"]=arriveData["Plan_date"].apply(getDayInMonth)

但是不知道从哪个版本开始就不行了,就会出现上面的报错。所以我们按照提示老实的改为:

arriveData.loc[:"dayInMonth"]=arriveData["Plan_date"].apply(getDayInMonth)

然而错误还是那个错误,我记得曾就有个阶段这种赋值还是可以的,只能说版本更新太快,或者是python的报错信息出现了bug吧,通过查文档可以发现,赋值方法早已经发生了改变:
一个DataFrame赋值的诡异报错 A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc_第2张图片
所以我就改成了这样:

arriveData.loc[:]["dayInMonth"]=arriveData["Plan_date"].apply(getDayInMonth)

然而还是不行,~~马上抑郁。。。吐血。从怀疑自己,到怀疑自己,到怀疑python。。。。经典的不知所措,最终灵感一现发现了问题。
在这里插入图片描述
原来我操作的数据来源于一个视图,我总是想在这个视图上进行数据的更改是不科学的,这样不利于数据维护。!哦,yes
在这里插入图片描述
改成上面这样之后,报错成功解决。

你可能感兴趣的:(工具使用,python,python,bug,pandas,dataFrame,报错)