. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the truth value of an array with more than one element is ambiguous. use a.any() or a.all() problem, pls let me know. This is what I do:
r = mlab.csv2rec(datafile, delimiter=',', names=COL_HEADERS)
mask = ((r["dt"] >= startdate) and (r["dt"] <= enddate))
selected = r[mask]
Thanks.
This error message suggests that you should use a.any() or a.all() instead of the and operator. However I usually solve this problem using the numpy.logical_and(a,b) method.
Here is the code after editing
Another method I think it is useful:
if np.array([]).size: print(1)
fixxed the error.if np.array([]) is not None: print(1)
Simply using a
numpy.logical_and(a,b)
would work. You could modify the code to be: