pandas报错Try using .loc[row_indexer,col_indexer] = value instead

D:\Program Files (x86)\Python37-32\lib\site-packages\pandas\core\indexing.py:845: 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
  self.obj[key] = _infer_fill_value(value)
D:\Program Files (x86)\Python37-32\lib\site-packages\pandas\core\indexing.py:966: 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
  self.obj[item] = s

示例源码

class demo_task():

    def __init__(self):
        pass

    def init_params(self, datas):
        os_task = OS_TASK()

        self._tsk = datas[datas.name.str.contains('Stk')]
        self._tsk.loc[:, 'prio'] = 0
        for idx in self._tsk.index:
            self._tsk.loc[idx, 'prio'] = os_task.find_prio(self._tsk.loc[idx, 'name'])
        print(self._tsk)

在上述中

self._tsk = datas[datas.name.str.contains('Stk')]

self._tsk.loc[:, 'prio'] = 0 # 此行会产生提示性错误,也就是产生链式赋值错误,为什么呢? datas切片给_tsk是一个视图,

这样可能导致datas也产生类似链式赋值,就产生上述错误,所以选择使用副本模式。

  • 视图模式
    • 将一个对象整体赋值给另一个变量
    • 修改一个变量,另一个变量值也会变
    • 多个变量数据指向同一内存数据
  • 副本模式
    • 将一个对象查询的一部分值赋值给另一个变量
    • 修改一个变量,另一个变量值不会变

这时你要注意数据来源了datas;

请使用 self._tsk = copy.copy(datas[datas.name.str.contains('Stk')])

就不会产生错误了。

你可能感兴趣的:(测试技术,技术内幕,Python)