numpy ValueError:无法将float NaN转换为integ

2024-05-15 00:44:57 发布

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

我试图设计一个np.where参数,它有以下输入

r_label = np.array([['Fiscal data as of Dec 31 2016', '2016', '2015', '2014'],
       ['Fiscal data as of Dec 31 2016', '2016', '2015', '2014']],
        dtype='<U29')

x = pd.Int64Index([12, 12], dtype='int64')

r_values_count = 2

check = np.isnat(pd.to_datetime(r_label[:,1:][0], errors='coerce', format='%b %d %Y')).all()

if_true =  [pd.to_datetime(r_label[:,1:][_], errors='coerce').map(lambda t: t.replace(month=x[_])) for _ in range(r_values_count)]

if_false = [pd.to_datetime(r_label[:,1:][_], errors='coerce', format='%b %d %Y') for _ in range(r_values_count)]

np.where(check, if_true, if_false)

但是我得到了错误ValueError: cannot convert float NaN to integernp.where参数的所有三个参数都能很好地独立工作,我不明白为什么它现在在{}内部工作,以及如何找到产生错误的代码的确切位置


Tags: todata参数datetimeifascountnp

热门问题