npross
npross

Reputation: 1888

Trying to find python numpy indexes with more than 2 logical conditions

Why does

w = data[np.where(np.logical_and(data['RA']>=(ra_in-0.1), data['RA']<=(ra_in+0.1)))]

work, but

w = data[np.where(np.logical_and(data['RA']>=(ra_in-0.1), data['RA']<=(ra_in+0.1), data['DEC']>=(dec_in-0.1), data['DEC']<=(dec_in+0.1)  ))]

doesn't??? This gives an "ValueError: invalid number of arguments" error. The 'in' values are floats; the data are numpy.ndarrays.

Upvotes: 0

Views: 325

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477190

The documentation of numpy.logical_and says:

numpy.logical_and(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

As you can see, except for additional parameters on how to perform the logical_and on the two array-like objects x1 and x2, there is no place for additional array-like objects. So in short logical_and only performs the logical and over two arrays.

You can use a cascade of logica_ands to perform the requested operation:

data[np.where(
    np.logical_and(
        np.logical_and(data['RA']>=(ra_in-0.1), data['RA']<=(ra_in+0.1),
        np.logical_and(data['DEC']>=(dec_in-0.1), data['DEC']<=(dec_in+0.1)
    ))]

So here we convert the invalid logical_and(A,B,C,D) into logical_and(logical_and(A,B),logical_and(C,D)).

Furthermore you can use the more elegant logical and &:

data[np.where(
    (data['RA']>=(ra_in-0.1)) & (data['RA']<=(ra_in+0.1)) & (data['DEC']>=(dec_in-0.1)) & (data['DEC']<=(dec_in+0.1))
    )]

Note that since & binds with higher priority, you need to add brackets. Note that however, we still have some cascading, since:

(data['RA']>=(ra_in-0.1)) & (data['RA']<=(ra_in+0.1)) & (data['DEC']>=(dec_in-0.1)) & (data['DEC']<=(dec_in+0.1))

is equivalent to:

(((data['RA']>=(ra_in-0.1)) & (data['RA']<=(ra_in+0.1))) & (data['DEC']>=(dec_in-0.1))) & (data['DEC']<=(dec_in+0.1))

or more abstract: A & B & C & D is equivalent to ((A & B) & C) & D.

Upvotes: 2

Related Questions