【发布时间】:2019-11-09 13:03:17
【问题描述】:
我已经创建了这个函数:
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 值转换为整数
npp_vec = np.vectorize(npp_tool)
npp = npp_vec(pb_opt, chlor_a, daylight, irrFunc, z_eu)
但是当我运行上面的代码时,我仍然收到错误消息“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"
我在我的函数中做错了什么导致这种情况?
【问题讨论】:
-
虽然不是直接的问题,但在使用
np.vectorize时要小心。注意它如何选择返回dtype,并考虑使用otypes参数。
标签: python python-3.x numpy nan