• Register
0 votes
1.4k 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)]
38.6k 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 281 views
281 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 809 views
809 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 568 views
568 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