如何处理 TypeError:输入类型不支持 ufunc 'isnan'

数据挖掘 熊猫 麻木的 海运
2021-09-16 04:27:24
I have dealt with all the Nan values in the features dataframe, then why I am still getting this error? 

sns.heatmap(features, annot=True, annot_kws={"size": 7})
sns.plt.show()

TypeError                                 Traceback (most recent call last)
    <ipython-input-11-534a699b432d> in <module>()
    ----> 1 sns.heatmap(features, annot=True, annot_kws={"size": 7})
          2 sns.plt.show()

    c:\users\jetjo\appdata\local\programs\python\python36\lib\site-packages\seaborn\matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
        515     plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
        516                           annot_kws, cbar, cbar_kws, xticklabels,
    --> 517                           yticklabels, mask)
        518 
        519     # Add the pcolormesh kwargs here

    c:\users\jetjo\appdata\local\programs\python\python36\lib\site-packages\seaborn\matrix.py in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
        166         # Determine good default values for the colormapping
        167         self._determine_cmap_params(plot_data, vmin, vmax,
    --> 168                                     cmap, center, robust)
        169 
        170         # Sort out the annotations

    c:\users\jetjo\appdata\local\programs\python\python36\lib\site-packages\seaborn\matrix.py in _determine_cmap_params(self, plot_data, vmin, vmax, cmap, center, robust)
        203                                cmap, center, robust):
        204         """Use some heuristics to set good defaults for colorbar and range."""
    --> 205         calc_data = plot_data.data[~np.isnan(plot_data.data)]
        206         if vmin is None:
        207             vmin = np.percentile(calc_data, 2) if robust else calc_data.min()

    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''
1个回答

您的数据框中可能存在对象类型的列。如果可能,将其删除或将其转换为浮点数、整数等

来自 Seaborn 的文档:

数据:矩形数据集

可以强制转换为 ndarray 的 2D 数据集。如果提供了 Pandas DataFrame,则索引/列信息将用于标记列和行。

所以你可以试试

sns.heatmap(features.drop(['columnName01_OfTypeObject','columnName02_OfTypeObject'],axis=1), annot=True, annot_kws={"size": 7})

这将暂时删除您的列。