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

前端 未结 6 2389
独厮守ぢ
独厮守ぢ 2020-11-21 04:59

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

6条回答
  •  轮回少年
    2020-11-21 05:09

    I had the same problem (i.e. indexing with multi-conditions, here it's finding data in a certain date range). The (a-b).any() or (a-b).all() seem not working, at least for me.

    Alternatively I found another solution which works perfectly for my desired functionality (The truth value of an array with more than one element is ambigous when trying to index an array).

    Instead of using suggested code above, simply using a numpy.logical_and(a,b) would work. Here you may want to rewrite the code as

    selected  = r[numpy.logical_and(r["dt"] >= startdate, r["dt"] <= enddate)]
    

提交回复
热议问题