np.where with np.logical_and crashes python

I am using numpy to build complex excel IF formula with the first condition being as follows:

DebitCond1 = np.where(np.logical_and(df['Debit']<0, df['Credit']<0, df['Debit']<df['Credit']))

Python crashes each time I run this (tried on multiple machines). Having two of the conditions are fine, but it's the combination of the three conditions together that seems to be at issue.

The error message is as follows:

Python has stopped working

A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.

Any thoughts on the cause and how to fix? Thanks!


Solution 1:

You are providing three values to np.logical_and(). The third is interpreted as the out= parameter, which you probably did not intend.

Here is the signature of np.logical_and()

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

and an excerpt from the documentation:

Compute the truth value of x1 AND x2 element-wise.

Parameters
----------
x1, x2 : array_like
    Input arrays.
    If ``x1.shape != x2.shape``, they must be broadcastable to a common
    shape (which becomes the shape of the output).
out : ndarray, None, or tuple of ndarray and None, optional
    A location into which the result is stored. If provided, it must have
    a shape that the inputs broadcast to. If not provided or None,
    a freshly-allocated array is returned. A tuple (possible only as a
    keyword argument) must have length equal to the number of outputs.