0

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.

1
  • Because logical_and takes two such vectors? All the remaining parameters are specifications on how to process the and. Commented Sep 16, 2017 at 19:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hmmm, (a) I could but (b) that code throws a syntax error, you need to close <=(ra_in+0.1)) and (dec_in+0.1)).
but, is this seriously the best way to do numpy logic? Cascades of logical_and's ??!!
@npross: no, I would not use a logical_and at all, unless you need some of the additional options. You can use &.
Nope. I get:: --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-199-8839202fb04c> in <module>() 1 data[np.where( ----> 2 data['RA']>=(ra_in-0.1) & data['RA']<=(ra_in+0.1) & data['DEC']>=(dec_in-0.1) & data['DEC']<=(dec_in+0.1) 3 )] TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
@nposs: yes, but you should add brackets. The & operator has precedence over <=.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.