• Register
0 votes
911 views

Problem :

I got following rather cryptic error message:
the truth value of an array with more than one element is ambiguous. use a.any() or a.all()
6.9k points

2 Answers

0 votes

Solution :

I had the same problem with indexing with multiple-conditions while finding the data in a certain date range. The (a-b).any() OR (a-b).all() was not working for me at all.

I found the solution which works perfectly for my functionality. 

Simply used the numpy.logical_and(a,b) and it worked for me. Following is the way to rewrite the code :

selected  = r[numpy.logical_and(r["dt"] >= startdate, r["dt"] <= enddate)]
36.1k points
0 votes

The error message explains itself very well.

ValueError: The truth value of an array with more than one element is ambiguous.

Use a.any() or a.all().

Solutions:

Array comparison return boolean:

Make sure your array comparison returns a boolean array. Methods any() and all() reduce values over the array either the value is logical_or or logical_and. Probably you also don’t need to check for equality.

What should bool return?

The main question is what should the bool(np.array([False, False, True])) have to return. There are many arguments.

  • True, because bool(np.array(x)) should have to return same as bool(list(x)), and non empty lists.
  • True, Because at least one element has to be true.
  • False, because all the elements are not false value.

Since, the truth value of an array having more than one element is ambiguous so you have to use .any() or .all().

Example:

>>> v = np.array([1, 2, 3]) == np.array([1, 2, 4])

>>> v

Array([True, True, False], dtype=bool)

>>> v.any()

True

>>> v.all()

False

Float values:

You have to use np.allclose, if you are making comparisons of array for float data type.

Example:

>>> np.allclose(np.array([1, 2, 3_1e-8]), np.array([1, 2, 3]))

True.

I hope you can understand and this answer will help to solve your problem.

3.9k points

Related questions

1 vote
2 answers 111 views
111 views
Anyone aware of this error. I am facing this issue in this function. It is not going to IF statement where I put my filter condition which is true then simply raise Error and without IF it is working but I want to put the filter condition which checks date between ... "value"]) It is not going to IF statement where I put my filter condition which is true then simply raise Error. Any suggestion?
asked May 8 Kashish
1 vote
2 answers 369 views
369 views
Problem : I am receiveing error as the truth value of a dataframe is ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all().
asked Nov 11, 2019 peterlaw 6.9k points
0 votes
1 answer 343 views
343 views
Problem : I am having below error in a title, and I don't know what's wrong. It was working when I used np.hstack instead of the np.append, but I want to make this faster, so using append. time_list the list of floats heights is the 1d np.array of ... <module> axe_x.append(time_list[np.arange(j+n,j+(time_interval-n))]) TypeError: only integer arrays with one element can be converted to an index
asked Jan 24 jwilliam 3.9k points
1 vote
1 answer 69 views
69 views
Problem : How to concatenate following numpy arrays? First np.array with the shape (5,4) as below : [[ 7487 500 389580 0] [ 7488 501 392994 0] [ 7491 508 389247 0] [ 7491 508 389247 0] [ 7492 502 399013 0]] Second np.array with the shape (1, ... ([array1, array2]) but i get below error ValueError: all the input arrays must have same number of dimensions How can I get the required output?
asked Dec 12, 2019 alecxe 7.5k points