Python numpy nanmean()';浮动';对象没有属性';数据类型';错误

Python numpy nanmean()';浮动';对象没有属性';数据类型';错误,python,numpy,mean,Python,Numpy,Mean,我需要一些帮助来处理上述错误。到目前为止,我的搜索没有返回任何解决方案。代码如下。对某些数据集有效,但对其他数据集则会引发错误 a = np.array(df_cols) aver = np.nanmean(a) 我正在使用Spyder 3.3.4 Python 3.7.3 64位| Qt 5.9.6 | PyQt5 5.9.2 | Windows 10 谢谢您的帮助。df_cols在问题案例中可能是对象dtypepandas自由使用此数据类型,如字符串或None内容 In [117]: np

我需要一些帮助来处理上述错误。到目前为止,我的搜索没有返回任何解决方案。代码如下。对某些数据集有效,但对其他数据集则会引发错误

a = np.array(df_cols)
aver = np.nanmean(a)
我正在使用Spyder 3.3.4 Python 3.7.3 64位| Qt 5.9.6 | PyQt5 5.9.2 | Windows 10


谢谢您的帮助。

df_cols
在问题案例中可能是
对象
dtype
pandas
自由使用此数据类型,如字符串或
None
内容

In [117]: np.nanmean(np.array([1.2,np.nan]))      # a float array                                         
Out[117]: 1.2

In [118]: np.nanmean(np.array([1.2,np.nan], object))   # object dtype array                    
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-118-ca706fd2a20e> in <module>
----> 1 np.nanmean(np.array([1.2,np.nan], object))

/usr/local/lib/python3.6/dist-packages/numpy/lib/nanfunctions.py in nanmean(a, axis, dtype, out, keepdims)
    914     cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=keepdims)
    915     tot = np.sum(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
--> 916     avg = _divide_by_count(tot, cnt, out=out)
    917 
    918     isbad = (cnt == 0)

/usr/local/lib/python3.6/dist-packages/numpy/lib/nanfunctions.py in _divide_by_count(a, b, out)
    188         else:
    189             if out is None:
--> 190                 return a.dtype.type(a / b)
    191             else:
    192                 # This is questionable, but currently a numpy scalar can

AttributeError: 'float' object has no attribute 'dtype'
[117]中的
:np.nanmean(np.array([1.2,np.nan])#一个浮点数组
Out[117]:1.2
在[118]中:np.nanmean(np.array([1.2,np.nan],object))#object-dtype数组
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
---->1 np.nanmean(np.array([1.2,np.nan],对象))
/nanmean中的usr/local/lib/python3.6/dist-packages/numpy/lib/nanfunctions.py(a、axis、dtype、out、keepdims)
914 cnt=np.sum(~mask,axis=axis,dtype=np.intp,keepdims=keepdims)
915总计=np.sum(arr,axis=axis,dtype=dtype,out=out,keepdims=keepdims)
-->916平均值=除以计数(tot、cnt、out=out)
917
918 isbad=(cnt==0)
/usr/local/lib/python3.6/dist-packages/numpy/lib/nanfunctions.py in除以计数(a,b,out)
188.其他:
189如果out为无:
-->190返回a.D类型类型(a/b)
191其他:
192#这是有问题的,但目前一个numpy标量可以
AttributeError:“float”对象没有属性“dtype”

在将df_列转换为系列后,我使用Pandas mean()解决了这个问题。Pandas mean将所有条目视为对象,并负责NaN。

这对我来说很有效

 a = np.array(df_cols)
 aver = np.nanmean(a,dtype='float32')

df_cols
中有什么?您似乎有一个数据帧。您能给我们一些行以便我们测试您的代码吗?谢谢您的回复。在将df_cols转换为级数后,我使用Pandas mean()解决了这个问题。Pandas mean将所有条目视为对象,并负责NaN。
 a = np.array(df_cols)
 aver = np.nanmean(a,dtype='float32')