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

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

在其 quiver plot 实现下受此 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 风格的标量还是布尔的 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天全站免登陆