numpy.where() 函数抛出一个 FutureWarning,返回标量而不是列表



我目前正在使用 NumPy 版本 1.12.1,每次调用numpy.where()都会返回一个空列表,并显示以下警告:

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison

我正在比较一个字符串,date_now和一个列表,dates_list

np.where(date_now==dates_list)

这会导致错误,因为程序随后调用期望 numpy.where(( 输出为非空的函数。有人对此有解决方案吗?

提前谢谢。

在当前的比较中,您正在将整个列表对象dates_list与字符串date_now进行比较。 这将导致逐元素比较失败并返回标量,就好像您只是比较两个标量值一样:

date_now = '2017-07-10'    
dates_list = ['2017-07-10', '2017-07-09', '2017-07-08']    
np.where(dates_list==date_now, True, False)
Out[3]: array(0)

您想要的是将dates_list声明为 NumPy 数组,以便于逐元素比较。

np.where(np.array(dates_list)==date_now, True, False)
Out[8]: array([ True, False, False], dtype=bool)

最新更新