• <small id='VymQx'></small><noframes id='VymQx'>

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

        ValueError:具有多个元素的数组的真值不明确.使用 a.any() 或 a.all()

        时间:2024-04-21

          • <bdo id='SYQI1'></bdo><ul id='SYQI1'></ul>

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

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

                  本文介绍了ValueError:具有多个元素的数组的真值不明确.使用 a.any() 或 a.all()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I just discovered a logical bug in my code which was causing all sorts of problems. I was inadvertently doing a bitwise AND instead of a logical AND.

                  I changed the code from:

                  r = mlab.csv2rec(datafile, delimiter=',', names=COL_HEADERS)
                  mask = ((r["dt"] >= startdate) & (r["dt"] <= enddate))
                  selected = r[mask]
                  

                  TO:

                  r = mlab.csv2rec(datafile, delimiter=',', names=COL_HEADERS)
                  mask = ((r["dt"] >= startdate) and (r["dt"] <= enddate))
                  selected = r[mask]
                  

                  To my surprise, I got the rather cryptic error message:

                  ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

                  Why was a similar error not emitted when I use a bitwise operation - and how do I fix this?

                  解决方案

                  r is a numpy (rec)array. So r["dt"] >= startdate is also a (boolean) array. For numpy arrays the & operation returns the elementwise-and of the two boolean arrays.

                  The NumPy developers felt there was no one commonly understood way to evaluate an array in boolean context: it could mean True if any element is True, or it could mean True if all elements are True, or True if the array has non-zero length, just to name three possibilities.

                  Since different users might have different needs and different assumptions, the NumPy developers refused to guess and instead decided to raise a ValueError whenever one tries to evaluate an array in boolean context. Applying and to two numpy arrays causes the two arrays to be evaluated in boolean context (by calling __bool__ in Python3 or __nonzero__ in Python2).

                  Your original code

                  mask = ((r["dt"] >= startdate) & (r["dt"] <= enddate))
                  selected = r[mask]
                  

                  looks correct. However, if you do want and, then instead of a and b use (a-b).any() or (a-b).all().

                  这篇关于ValueError:具有多个元素的数组的真值不明确.使用 a.any() 或 a.all()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 dict 重新映射 pandas 列中的值,保留 NaN 下一篇:Python 2 如何比较字符串和整数?为什么列表比较大于数字,而元组大于列表?

                  相关文章

                  1. <legend id='n5aPt'><style id='n5aPt'><dir id='n5aPt'><q id='n5aPt'></q></dir></style></legend>

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

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

                      <tfoot id='n5aPt'></tfoot>