FutureWarning:元素比较失败;返回标量,但将来会执行元素比较 [英] FutureWarning: elementwise comparison failed; returning scalar, but in the future will perform elementwise comparison

查看:38
本文介绍了FutureWarning:元素比较失败;返回标量,但将来会执行元素比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 3 上使用 Pandas 0.19.1.我收到有关这些代码行的警告.我正在尝试获取一个包含所有行号的列表,其中字符串 Peter 出现在 Unnamed: 5 列中.

I am using Pandas 0.19.1 on Python 3. I am getting a warning on these lines of code. I'm trying to get a list that contains all the row numbers where string Peter is present at column Unnamed: 5.

df = pd.read_excel(xls_path)
myRows = df[df['Unnamed: 5'] == 'Peter'].index.tolist()

它产生一个警告:

"Python36libsite-packagespandascoreops.py:792: FutureWarning: elementwise 
comparison failed; returning scalar, but in the future will perform 
elementwise comparison 
result = getattr(x, name)(y)"

这是什么 FutureWarning,我应该忽略它,因为它似乎有效.

What is this FutureWarning and should I ignore it since it seems to work.

推荐答案

这个 FutureWarning 不是来自 Pandas,而是来自 numpy 并且该 bug 也影响 matplotlib 和其他人,这里是如何重现警告更接近于问题的根源:

import numpy as np
print(np.__version__)   # Numpy version '1.12.0'
'x' in np.arange(5)       #Future warning thrown here

FutureWarning: elementwise comparison failed; returning scalar instead, but in the 
future will perform elementwise comparison
False

使用双等于运算符重现此错误的另一种方法:

Another way to reproduce this bug using the double equals operator:

import numpy as np
np.arange(5) == np.arange(5).astype(str)    #FutureWarning thrown here

在其箭袋图实现下受此 FutureWarning 影响的 Matplotlib 示例:https://matplotlib.org/examples/pylab_examples/quiver_demo.html

An example of Matplotlib affected by this FutureWarning under their quiver plot implementation: https://matplotlib.org/examples/pylab_examples/quiver_demo.html

Numpy 和原生 python 之间在将字符串与 numpy 的数字类型进行比较时会发生什么方面存在分歧.注意右边的操作数是python的草皮,一个原始字符串,中间的操作是python的草皮,而左边的操作数是numpy的草皮.你应该返回 Python 风格的 Scalar 还是 Numpy 风格的 ndarray 布尔值?Numpy 说 ndarray of bool,Pythonic 开发人员不同意.经典对峙.

There is a disagreement between Numpy and native python on what should happen when you compare a strings to numpy's numeric types. Notice the right operand is python's turf, a primitive string, and the middle operation is python's turf, but the left operand is numpy's turf. Should you return a Python style Scalar or a Numpy style ndarray of Boolean? Numpy says ndarray of bool, Pythonic developers disagree. Classic standoff.

如果项目存在于数组中,应该是元素比较还是标量?

Should it be elementwise comparison or Scalar if item exists in the array?

如果您的代码或库使用 in== 运算符将 python 字符串与 numpy ndarrays 进行比较,则它们不兼容,因此当您尝试时,它返回一个标量,但只是现在.警告表明,如果 python/numpy 决定采用 Numpy 风格,这种行为将来可能会发生变化,因此您的代码会在整个地毯上呕吐.

If your code or library is using the in or == operators to compare python string to numpy ndarrays, they aren't compatible, so when if you try it, it returns a scalar, but only for now. The Warning indicates that in the future this behavior might change so your code pukes all over the carpet if python/numpy decide to do adopt Numpy style.

Numpy 和 Python 处于对峙状态,目前该操作返回一个标量,但未来可能会发生变化.

Numpy and Python are in a standoff, for now the operation returns a scalar, but in the future it may change.

https://github.com/numpy/numpy/issues/6784

https://github.com/pandas-dev/pandas/issues/7830

要么锁定您的 python 和 numpy 版本,忽略警告并期望行为不会改变,要么将 ==in 的左右操作数转换为来自 numpy 类型或原始 python 数字类型.

Either lockdown your version of python and numpy, ignore the warnings and expect the behavior to not change, or convert both left and right operands of == and in to be from a numpy type or primitive python numeric type.

全局抑制警告:

import warnings
import numpy as np
warnings.simplefilter(action='ignore', category=FutureWarning)
print('x' in np.arange(5))   #returns False, without Warning

逐行抑制警告.

import warnings
import numpy as np

with warnings.catch_warnings():
    warnings.simplefilter(action='ignore', category=FutureWarning)
    print('x' in np.arange(2))   #returns False, warning is suppressed

print('x' in np.arange(10))   #returns False, Throws FutureWarning

只需按名称抑制警告,然后在旁边大声评论,提及当前版本的 python 和 numpy,说这段代码很脆弱,需要这些版本,然后放一个链接到这里.在路上踢罐头.

Just suppress the warning by name, then put a loud comment next to it mentioning the current version of python and numpy, saying this code is brittle and requires these versions and put a link to here. Kick the can down the road.

TLDR: pandas 是绝地;numpy 是小屋;python 是银河帝国.

TLDR: pandas are Jedi; numpy are the hutts; and python is the galactic empire.

这篇关于FutureWarning:元素比较失败;返回标量,但将来会执行元素比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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