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

查看:388
本文介绍了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()

它会产生警告:

"\Python36\lib\site-packages\pandas\core\ops.py:792: FutureWarning: elementwise 
comparison failed; returning scalar, but in the future will perform 
elementwise comparison 
result = getattr(x, name)(y)"

这是什么FutureFarning,由于它似乎起作用,因此我应该忽略它.

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

推荐答案

此FutureWarning并非来自Pandas,而是来自numpy,并且该错误也影响了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

使用double equals运算符重现此错误的另一种方法:

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的数字类型进行比较时,Numpy和本机python之间应该发生什么不一致.请注意,左操作数是python的草皮,是原始字符串,中间操作是python的草皮,而右操作数是numpy的草皮.您应该返回Python样式的Scalar还是Numpy样式的boolean布尔值? Numpy说布尔的ndarray,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 left operand is python's turf, a primitive string, and the middle operation is python's turf, but the right 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.

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

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是银河帝国. https://youtu.be/OZczsiCfQQk?t=3

TLDR: pandas are Jedi; numpy are the hutts; and python is the galactic empire. https://youtu.be/OZczsiCfQQk?t=3

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

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