• <legend id='wxnOy'><style id='wxnOy'><dir id='wxnOy'><q id='wxnOy'></q></dir></style></legend>
    <tfoot id='wxnOy'></tfoot>

    <i id='wxnOy'><tr id='wxnOy'><dt id='wxnOy'><q id='wxnOy'><span id='wxnOy'><b id='wxnOy'><form id='wxnOy'><ins id='wxnOy'></ins><ul id='wxnOy'></ul><sub id='wxnOy'></sub></form><legend id='wxnOy'></legend><bdo id='wxnOy'><pre id='wxnOy'><center id='wxnOy'></center></pre></bdo></b><th id='wxnOy'></th></span></q></dt></tr></i><div id='wxnOy'><tfoot id='wxnOy'></tfoot><dl id='wxnOy'><fieldset id='wxnOy'></fieldset></dl></div>
      <bdo id='wxnOy'></bdo><ul id='wxnOy'></ul>

        <small id='wxnOy'></small><noframes id='wxnOy'>

        FutureWarning:元素比较失败;返回标量,但将来将执行元素比较

        FutureWarning: elementwise comparison failed; returning scalar, but in the future will perform elementwise comparison(FutureWarning:元素比较失败;返回标量,但将来将执行元素比较)
        <legend id='bX3FS'><style id='bX3FS'><dir id='bX3FS'><q id='bX3FS'></q></dir></style></legend>

            <tbody id='bX3FS'></tbody>
            • <small id='bX3FS'></small><noframes id='bX3FS'>

                <i id='bX3FS'><tr id='bX3FS'><dt id='bX3FS'><q id='bX3FS'><span id='bX3FS'><b id='bX3FS'><form id='bX3FS'><ins id='bX3FS'></ins><ul id='bX3FS'></ul><sub id='bX3FS'></sub></form><legend id='bX3FS'></legend><bdo id='bX3FS'><pre id='bX3FS'><center id='bX3FS'></center></pre></bdo></b><th id='bX3FS'></th></span></q></dt></tr></i><div id='bX3FS'><tfoot id='bX3FS'></tfoot><dl id='bX3FS'><fieldset id='bX3FS'></fieldset></dl></div>
                  <bdo id='bX3FS'></bdo><ul id='bX3FS'></ul>

                  <tfoot id='bX3FS'></tfoot>
                • 本文介绍了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:元素比较失败;返回标量,但将来将执行元素比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <small id='HgPvq'></small><noframes id='HgPvq'>

                        <tbody id='HgPvq'></tbody>
                      • <bdo id='HgPvq'></bdo><ul id='HgPvq'></ul>
                        <i id='HgPvq'><tr id='HgPvq'><dt id='HgPvq'><q id='HgPvq'><span id='HgPvq'><b id='HgPvq'><form id='HgPvq'><ins id='HgPvq'></ins><ul id='HgPvq'></ul><sub id='HgPvq'></sub></form><legend id='HgPvq'></legend><bdo id='HgPvq'><pre id='HgPvq'><center id='HgPvq'></center></pre></bdo></b><th id='HgPvq'></th></span></q></dt></tr></i><div id='HgPvq'><tfoot id='HgPvq'></tfoot><dl id='HgPvq'><fieldset id='HgPvq'></fieldset></dl></div>

                      • <legend id='HgPvq'><style id='HgPvq'><dir id='HgPvq'><q id='HgPvq'></q></dir></style></legend>
                      • <tfoot id='HgPvq'></tfoot>