Specifically silent Pandas SettingWithCopyWarning 使用警告上下文管理器?
Specificaly silent Pandas SettingWithCopyWarning using warnings context manager?
我有显示所有警告的策略:
import warnings
warnings.simplefilter('always')
我想使用上下文管理器消除一些误报 Pandas 警告:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=SettingWithCopyWarning)
# Some assignment raising false positive warning that should be silenced
# Some assignment actually raising a true positive warning
但是在查看 Pandas source 之后,我找不到对象 SettingWithCopyWarning
在 Pandas 中定义的位置。
有人知道这个对象在 Pandas 命名空间中的什么地方定义吗?
以下应该满足您的需求:
pd.set_option('mode.chained_assignment', None)
取自https://www.dataquest.io/blog/settingwithcopywarning/
但是,请花一些时间阅读上面的文章,因为它对那个警告做了很多解释。也许你不想一直沉默吧!
将评论中的信息合并为一个答案:
import warnings
import pandas as pd
正如@Andrew 指出的那样,我可以使用专用的 Pandas 上下文管理器来实现它:
with pd.option_context('mode.chained_assignment', None):
# Chaining Assignment, etc...
或者使用 PSL warnings
提供我可以找到警告 SettingWithCopyWarning
对象(感谢@coldspeed 的 GitHub link):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=pd.core.common.SettingWithCopyWarning)
# Chaining Assignment, etc...
请注意,这两种解决方案的行为似乎相似,但并不完全等同:
- Pandas 上下文管理器临时更改 Pandas 选项然后恢复它;
- PSL Context Manager 捕获特定警告并在不更改 Pandas 选项的情况下将其静音。
附加信息
值得将此特定警告转换为错误:
pd.set_option('mode.chained_assignment', 'raise')
这将迫使您的开发避免那些特定的边缘情况,并强制您的代码明确说明它是在视图上工作还是仅在副本上工作。
当然可以像往常一样捕获异常:
try:
# Chaining Assignment, etc...
except pd.core.common.SettingWithCopyError:
pass
但在这种情况下,将警告转换为错误可能会迫使您修改不明确的代码,直到错误消失,而不是捕获相关的异常。
观察
恕我直言,使用这些警告完全静音:
pd.set_option('mode.chained_assignment', None)
这是一种不好的做法,无助于生成更好的代码。
我有显示所有警告的策略:
import warnings
warnings.simplefilter('always')
我想使用上下文管理器消除一些误报 Pandas 警告:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=SettingWithCopyWarning)
# Some assignment raising false positive warning that should be silenced
# Some assignment actually raising a true positive warning
但是在查看 Pandas source 之后,我找不到对象 SettingWithCopyWarning
在 Pandas 中定义的位置。
有人知道这个对象在 Pandas 命名空间中的什么地方定义吗?
以下应该满足您的需求:
pd.set_option('mode.chained_assignment', None)
取自https://www.dataquest.io/blog/settingwithcopywarning/
但是,请花一些时间阅读上面的文章,因为它对那个警告做了很多解释。也许你不想一直沉默吧!
将评论中的信息合并为一个答案:
import warnings
import pandas as pd
正如@Andrew 指出的那样,我可以使用专用的 Pandas 上下文管理器来实现它:
with pd.option_context('mode.chained_assignment', None):
# Chaining Assignment, etc...
或者使用 PSL warnings
提供我可以找到警告 SettingWithCopyWarning
对象(感谢@coldspeed 的 GitHub link):
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=pd.core.common.SettingWithCopyWarning)
# Chaining Assignment, etc...
请注意,这两种解决方案的行为似乎相似,但并不完全等同:
- Pandas 上下文管理器临时更改 Pandas 选项然后恢复它;
- PSL Context Manager 捕获特定警告并在不更改 Pandas 选项的情况下将其静音。
附加信息
值得将此特定警告转换为错误:
pd.set_option('mode.chained_assignment', 'raise')
这将迫使您的开发避免那些特定的边缘情况,并强制您的代码明确说明它是在视图上工作还是仅在副本上工作。
当然可以像往常一样捕获异常:
try:
# Chaining Assignment, etc...
except pd.core.common.SettingWithCopyError:
pass
但在这种情况下,将警告转换为错误可能会迫使您修改不明确的代码,直到错误消失,而不是捕获相关的异常。
观察
恕我直言,使用这些警告完全静音:
pd.set_option('mode.chained_assignment', None)
这是一种不好的做法,无助于生成更好的代码。