是什么导致我的函数中出现“ValueError:cannot convert float NaN to integer”

2024-05-04 19:35:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了这个函数:

import numpy as np 


def npp_tool(pb_opt, chlor_a, daylight, irrFunc, z_eu):
            if daylight == 0 or daylight == np.nan:
                return -32767
            elif pb_opt == np.nan:
                return -32767
            elif chlor_a == -32767 or daylight == np.nan:
                return -32767
            elif irrFunc == np.nan:
                return -32767
            elif z_eu == np.nan:
                return -32767
            else:
                return pb_opt * chlor_a * daylight * irrFunc * z_eu

会转变的np.nan公司整数输入中的值

^{pr2}$

但是当我运行上面的代码时,我仍然得到错误消息“ValueError:cannot convert float NaN to integer”:

"---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-27-7ad956e7f0a2> in <module>
----> 1 npp = npp_vec(pb_opt, chlor_a, daylight, irrFunc, z_eu)

~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\function_base.py in __call__(self, *args, **kwargs)
   2089             vargs.extend([kwargs[_n] for _n in names])
   2090 
-> 2091         return self._vectorize_call(func=func, args=vargs)
   2092 
   2093     def _get_ufunc_and_otypes(self, func, args):

~\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\lib\function_base.py in _vectorize_call(self, func, args)
   2168 
   2169             if ufunc.nout == 1:
-> 2170                 res = array(outputs, copy=False, subok=True, dtype=otypes[0])
   2171             else:
   2172                 res = tuple([array(x, copy=False, subok=True, dtype=t)

ValueError: cannot convert float NaN to integer" 

我在我的职能上做错了什么导致了这个问题?在


Tags: inselfreturnlibnpargsnancall
1条回答
网友
1楼 · 发布于 2024-05-04 19:35:30

不能使用==比较np.nan和{}

您应该使用^{}

因此,将所有比较改为:

elif np.isnan(pb_opt):

等等

例如:

^{pr2}$

因此,上述比较失败,而isnan起作用:

In[73]:
np.isnan(np.nan)

Out[72]: True

NaN具有无法与其自身比较的属性:

In[73]:
np.nan != np.nan

Out[73]: True

相关问题 更多 >