deepak deepak - 11 months ago 49
Python Question

How to search for rows using an expression in numpy rec array

Consider i have a numpy recarray as below:

r = np.rec.array([(1,2,'Hello'),(2,3,"World")], \
dtype=[('foo', 'i4'),('bar', 'i4'), ('baz', 'S10')])

Now i want to get all the object that returns 'True' for the following expression stored in dict,

expression = {'foo':1,'bar':3.,'baz':'Hello'}

I am looking for a method like**expression) to select me all the objects that match those conditions passed to the select.

expression is dynamic, changes very frequently.


You can combine array comparisons with boolean operators. For example (with a poor-mans implementations of a float comparison):

EPS = 1e-7
mask = np.ones(len(r), dtype=np.bool)
mask &= r['foo'] == 1
mask &= (r['bar'] > 3-EPS) & (r['bar'] < 3+EPS)
mask &= r['baz'] == 'hello'

Or you can use or, | instead of and, &, if you prefer that. (Or a combination, since the two r['bar'] will have to be &-ed anyway.)