簡體   English   中英

np.where 與 np.logical_and 崩潰 python

[英]np.where with np.logical_and crashes python

我正在使用 numpy 構建復雜的 excel IF 公式,第一個條件如下:

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

Python 每次運行時都會崩潰(在多台機器上嘗試過)。 有兩個條件很好,但是這三個條件的組合似乎有問題。

錯誤信息如下:

Python 已停止工作

一個問題導致程序停止正常工作。 Windows 將關閉程序並通知您是否有可用的解決方案。

關於原因以及如何解決的任何想法? 謝謝!

您正在為np.logical_and()提供三個值。 第三個被解釋為out=參數,您可能並不打算這樣做。

這是np.logical_and()的簽名

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

以及文檔摘錄:

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.

這個答案通過鏈接條件、 np.reduce它們、 functools.reduce它們或用顯式軸替換所有選項來完全解決上述情況。

像這樣

DebitCond1a=np.where(np.logical_and(df['P1_DR']<0, df['P1_DR']<df['P1_CR']))
DebitCond1b=np.where(df['P1_CR']<0)

DebitCond1=np.where(np.logical_and(DebitCond1a,DebitCond1b))

是在這種情況下合並超過 2 個條件的一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM