0 votes

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.

I changed the code from:

```r = mlab.csv2rec(datafile, delimiter=',', names=COL_HEADERS)
mask = ((r["dt"] >= startdate) & (r["dt"] <= enddate))
selected = r[mask]```

TO:

```r = mlab.csv2rec(datafile, delimiter=',', names=COL_HEADERS)
mask = ((r["dt"] >= startdate) and (r["dt"] <= enddate))
selected = r[mask]```

To my surprise, I got the rather cryptic error message:

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

Why was a similar error not emitted when I use a bitwise operation - and how do I fix this? Jun 15, 2020 in Python 15,452 views

## 1 answer to this question.

0 votes

Hello @kartik,

Since different users might have different needs and different assumptions, the NumPy developers refused to guess and instead decided to raise a ValueError whenever one tries to evaluate an array in boolean context. Applying and to two numpy arrays causes the two arrays to be evaluated in boolean context (by calling __bool__ in Python3 or __nonzero__ in Python2).

Your original code

```mask = ((r["dt"] >= startdate) & (r["dt"] <= enddate))
selected = r[mask]```

looks correct. However, if you do want and, then instead of a and b use (a-b).any() or (a-b).all().

Hope it helps! answered Jun 15, 2020 by
• 82,760 points

0 votes
0 answers

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

I couldn't figure out the problem. here my ...READ MORE

0 votes
1 answer

## How to find the occurrences of any word with more than two vowels in a file using regex?

for word in read: <--- iterating ...READ MORE

0 votes
0 answers

## i want to create a dataframe,everytime i am gonna parse a xml file ,my expectation is all AIN.MFN and REG should come d contain all of themfrom that file but it is returning a dataframe with one row

from xml.dom import minidom import pandas as pd mydoc ...READ MORE

0 votes
1 answer

## How to slice an array using python numpy? Is there any numpy tutorial which has covered all its operations?

Slicing is basically extracting particular set of ...READ MORE

0 votes
2 answers

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote
2 answers

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

0 votes
0 answers

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE

+5 votes
6 answers

## Lowercase in Python

You can simply the built-in function in ...READ MORE

0 votes
1 answer

## How to override the default value of a Model Field from an Abstract Base Class

Hello @kartik, You can do this as follows: class ...READ MORE

0 votes
1 answer

## How do I sort a list of dictionaries by a value of the dictionary?

Hello @kartik, import operator To sort the list of ...READ MORE