[Python Debug] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

I Got a SettingWithCopyWarning when I ran the following code:

tmp=date[date[‘date‘].isnull().values==True]
tmp[‘date‘]=tmp[‘text‘].str.extract(regex2,re.VERBOSE)

The details of the Warning are :

/Users/monica/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:23: 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

Then I got the inspiration from

https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas

The SettingWithCopyWarning was created to flag potentially confusing "chained" assignments such as the following, which don‘t always work as expected, particularly when the first selection returns a copy.

df[df[‘A‘] > 2][‘B‘] = new_val  # new_val not set in df

The warning offers a suggestion to rewrite as follows:

df.loc[df[‘A‘] > 2, ‘B‘] = new_val

Or you can safely disable this new warning with the following assignment.

pd.options.mode.chained_assignment = None  # default=‘warn‘

Since we copied the dataframe firstly, there are some other options.

You can set the is_copy flag to False, which will effectively turn off the check, for that object:

tmp.is_copy = False

Or explicitly copy then no further warning will happen.

In my case, the problem was solved by add .copy( ) method while defining tmp, i.e.,

tmp=date[date[‘date‘].isnull().values==True].copy()
tmp[‘date‘]=tmp[‘text‘].str.extract(regex2,re.VERBOSE)

原文地址:https://www.cnblogs.com/sherrydatascience/p/10291262.html

时间: 2024-10-11 06:00:54

[Python Debug] SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.的相关文章

Python之美[从菜鸟到高手]--浅拷贝、深拷贝完全解读(copy源码分析)

可悲的我一直以为copy模块是用C写的,有时候需要深入了解deepcopy,文档描述的实在太简单,还是不知所云. 比如说最近看sqlmap源码中AttribDict的_deepcopy__有些疑惑, def __deepcopy__(self, memo): retVal = self.__class__() memo[id(self)] = retVal for attr in dir(self): if not attr.startswith('_'): value = getattr(se

python debug ——vscode

vscode 是微软开发的编辑器,添加python插件就可以直接运行python脚本,也可以添加Jupyter插件用"#%%"魔术在开始行注释后 运行在python kernel . 前提是已经安装好python,ipython,建议使用anaconda安装包. 安装python支持插件后,在脚本打开后点击最左边调试键,绿色开始键就可以进入debug模式了. 有变量,和表达式跟踪监视,比pycharm要简便多了

python debug小技巧&&工程能力的几点建议

Debug小技巧: 转载请声明本文的引用出处:仰望大牛的小清新 1.初次编程时,在每一个if后面都写上else,这样,如果你的else原本是不应该运行的,那么就可以在else中输出此时的状态信息便于排查bug 2.不要嵌套太多的if(比如超过2层),因为嵌套多意味着需要来回查看不同层次的条件,尝试用一层if来做 3.像对待段落一样对待if-else,在它们开头和结尾加上空行,而把if-elif-else看作段落中的句子4.if中的bool表达式应尽量简洁,如果态复杂,就用变量保存起来,并且给变量

python Debug 单步调试

一直犯愁的是python的调试,曾经写c都是编译完了用gdb直接调试了,轻松愉快.如今遇到这么一个解释型的程序.不知道怎么办了.用log吧,有时就是一个小程序,不想写这么多代码.打屏吧.有时屏幕翻得快,也是挺郁闷的.今天最终好了,看到了一个实用的东西.PDB 方法一:执行 python -m pdb myscript.py (Pdb) 会自己主动停在第一行.等待调试,这时你能够看看 帮助 (Pdb) h 说明下这几个关键 命令 <断点设置 (Pdb)b 10 #断点设置在本py的第10行 或(P

Python debug

Python debug——TypeError unhashable type(list/set/dict)

正如错误提示,list/set/dict 均不可被哈希. 这一异常通常出现在,调用 set(-) 来构造一个 set (集合类型)时,set() 需要传递进来可哈希的元素(hashable items). (1)list.set.dict:是不可哈希的 >>> list.__hash__ None >>> set.__hash__ None >>> dict.__hash__ None 1 2 3 4 5 6 (2)int.float.str.tupl

Python全栈__数据类型的补充、集合set、深浅copy

1.数据类型的补充 1.1 元组 当元组里面只有一个元素且没有逗号时,则该数据的数据类型与括号里面的元素相同. tu1 = ('laonanhai') tu2 = ('laonanhai') print(tu1, type(tu1)) print(tu2, type(tu2),) tu1 = (1) tu2 = (1,) print(tu1, type(tu1)) print(tu2, type(tu2)) tu1 = ([1, 2, 3]) tu2 = ([1, 2, 3],) print(t

pandas DataFrame 警告(SettingWithCopyWarning)

刚接触python不久,编程也是三脚猫,所以对常用的这几个工具还没有一个好的使用习惯,毕竟程序语言是头顺毛驴.所以最近在工作中使用的时候在使用pandas的DataFrame时遇到了以下报警: 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 insteadSee the

Python pandas 0.19.1 Indexing and Selecting Data文档翻译

最近在写个性化推荐的论文,经常用到Python来处理数据,被pandas和numpy中的数据选取和索引问题绕的比较迷糊,索性把这篇官方文档翻译出来,方便自查和学习,翻译过程中难免很多不到位的地方,但大致能看懂,错误之处欢迎指正~ Python pandas 0.19.1 Indexing and Selecting Data 原文链接 http://pandas.pydata.org/pandas-docs/stable/indexing.html 数据索引和选取 pandas对象中的轴标签信息