**Problem :**

**the truth value of an array with more than one element is ambiguous. use a.any() or a.all()**

- Q&A
- Questions
- Unanswered
- Tags
- Users
- Ask a Question
- Articles
- Interview Questions
- Programming Quiz
- Post An Article

0 votes

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)]

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().
```

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.

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().

```
>>> v = np.array([1, 2, 3]) == np.array([1, 2, 4])
>>> v
Array([True, True, False], dtype=bool)
>>> v.any()
True
>>> v.all()
False
```

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

```
>>> 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.