Python nansum工作时在nanmax中指定轴时出现Numpy错误
我正在尝试获得numpy.array的最大值,如下代码所示:Python nansum工作时在nanmax中指定轴时出现Numpy错误,python,python-3.x,numpy,max,nan,Python,Python 3.x,Numpy,Max,Nan,我正在尝试获得numpy.array的最大值,如下代码所示: [np.nanmax(temp_data[i:i+window_size, 1:], axis=0) for i, t in enumerate(temp_data)] 但是,我得到了以下错误: {TypeError}ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported
[np.nanmax(temp_data[i:i+window_size, 1:], axis=0) for i, t in enumerate(temp_data)]
但是,我得到了以下错误:
{TypeError}ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
无论如何,如果我使用的不是nanmax
,而是nansum
,就像下面的代码一样,一切都很顺利:
[np.nansum(temp_data[i:i+window_size, 1:], axis=0) for i, t in enumerate(temp_data)]
=> A 9x2 requested np.array
此外,这也适用于代码,无论是nanmax
还是nansum
都可以工作:
[np.nansum(temp_data[i:i+window_size, 1:]) for i, t in enumerate(temp_data)]
[np.nanmax(temp_data[i:i+window_size, 1:]) for i, t in enumerate(temp_data)]
=> A 9x1 np.array, (but this is not what I want)
知道了吗,为什么在nanmax
中指定轴在为nansum
工作时失败了
编辑:临时数据示例:
temp_data = np.array([[datetime.datetime(1980, 1, 1, 0, 0), np.nan, np.nan],
[datetime.datetime(1980, 1, 2, 0, 0), np.nan, np.nan],
[datetime.datetime(1980, 1, 3, 0, 0), np.nan, np.nan],
[datetime.datetime(1980, 1, 4, 0, 0), np.nan, np.nan],
[datetime.datetime(1980, 1, 7, 0, 0), np.nan, 1],
[datetime.datetime(1980, 1, 8, 0, 0), np.nan, 2],
[datetime.datetime(1980, 1, 9, 0, 0), 1, 3],
[datetime.datetime(1980, 1, 10, 0, 0), 5, 4],
[datetime.datetime(1980, 1, 11, 0, 0), 4, 1]])
您可以强制对数据进行
float
转换,因为当numpy拥有对象时,他们似乎不知道如何处理数据
# \/
>>> [np.nanmax(temp_data[i:i+window_size, 1:].astype(float), axis=0) for i, t in enumerate(temp_data)]
[array([ nan, nan]),
array([ nan, nan]),
array([ nan, nan]),
array([ nan, 1.]),
array([ nan, 2.]),
array([ 1., 3.]),
array([ 5., 4.]),
array([ 5., 4.]),
array([ 4., 1.])]
这可能是一个bug,也可能是numpy中未定义行为的结果。您的数组temp\u数据
具有数据类型对象
。这意味着数组中的值可以是任意Python对象。您看到的错误是将对象数组指定给nanmax
并指定轴时发生的错误
下面是一个使用numpy版本1.12.1的简单示例:
In [21]: a = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=object)
In [22]: np.nanmax(a, axis=0)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-a020f98a2536> in <module>()
----> 1 np.nanmax(a, axis=0)
/Users/warren/miniconda3scipy/lib/python3.5/site-packages/numpy/lib/nanfunctions.py in nanmax(a, axis, out, keepdims)
343 # Fast, but not safe for subclasses of ndarray
344 res = np.fmax.reduce(a, axis=axis, out=out, **kwargs)
--> 345 if np.isnan(res).any():
346 warnings.warn("All-NaN slice encountered", RuntimeWarning, stacklevel=2)
347 else:
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
nansum()
正确处理轴:
In [24]: np.nansum(a, axis=0)
Out[24]: array([4.0, 6.0], dtype=object)
如果数组中的对象都可转换为Python浮点值,则可以使用astype()
方法将数组转换为数据类型为numpy.float64
的数组,其中nanmax()
按预期工作:
In [26]: a.astype(np.float64)
Out[26]:
array([[ 1., 2.],
[ 3., 4.]])
In [27]: np.nanmax(a.astype(np.float64), axis=0)
Out[27]: array([ 3., 4.])
temp_data
的数据类型是什么?@Divakar:我用temp_data
的样本更新了我的问题,你是如何导入datetime
的?导入datetime,但我的datetime没有问题,它们在代码的其余部分工作得很好,由于我只在第二个维度上取1:
,因此被排除在nanmax
计算之外。仅供参考:我为基础问题创建了一个numpy问题:
In [26]: a.astype(np.float64)
Out[26]:
array([[ 1., 2.],
[ 3., 4.]])
In [27]: np.nanmax(a.astype(np.float64), axis=0)
Out[27]: array([ 3., 4.])