UserWarning:将遮罩的元素转换为nan [英] UserWarning: converting a masked element to nan

查看:215
本文介绍了UserWarning:将遮罩的元素转换为nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的执行python脚本(很长一段时间都包含在这里)导致出现警告消息.我不知道在代码的哪一行被引发.我如何获得此信息?

Executing a python script (way to long to include here) I wrote leads to a warning message. I don't know at which line in my code this gets raised. How can I get this information?

此外,这到底是什么意思?实际上,我不知道自己在使用某种蒙版数组吗?

Furthermore, what does this mean exactly? In fact, I didn't know I was using a masked array of some sort?

/usr/lib/pymodules/python2.7/numpy/ma/core.py:3785: UserWarning: Warning: converting a masked element to nan.
warnings.warn("Warning: converting a masked element to nan.")

推荐答案

您可以使用 warnings 模块将警告转换为异常.最简单的方法称为simplefilter.这是一个例子.生成警告的代码在func2b()中,因此存在非平凡的回溯.

You can use the warnings module to convert warnings to exceptions. The simplest method is called simplefilter. Here's an example; the code that generates the warning is in func2b(), so there is a nontrival traceback.

import warnings


def func1():
    print "func1"

def func2():
    func2b()
    print "func2"

def func2b():
    warnings.warn("uh oh")

def func3():
    print "func3"


if __name__ == "__main__":
    # Comment the following line to see the default behavior.
    warnings.simplefilter('error', UserWarning)
    func1()
    func2()
    func3()

当包含对simplefilter的调用的行被注释掉时,输出为

When the line containing the call to simplefilter is commented out, the output is

func1
warning_to_exception.py:13: UserWarning: uh oh
  warnings.warn("uh oh")
func2
func3

包括该行,您将获得一个追溯:

With that line included, you get a traceback:

func1
Traceback (most recent call last):
  File "warning_to_exception.py", line 23, in <module>
    func2()
  File "warning_to_exception.py", line 9, in func2
    func2b()
  File "warning_to_exception.py", line 13, in func2b
    warnings.warn("uh oh")
UserWarning: uh oh

这篇关于UserWarning:将遮罩的元素转换为nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆