pandas 类型错误:输入类型不支持 ufunc 'isnan',-seaborn 热图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43523115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: ufunc 'isnan' not supported for the input types, - seaborn Heatmap
提问by SA.
I am getting the error when i try to plot a seaborn heatmap
当我尝试绘制 seaborn 热图时出现错误
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''
类型错误:输入类型不支持 ufunc 'isnan',并且无法根据转换规则 ''safe'' 将输入安全地强制转换为任何受支持的类型
my code is as follows
我的代码如下
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
df = pd.read_table(r"C:\Results.CST", sep='\s+',header=11, engine = 'python')
df2 = cridim[['Bottom','Location_X','Location_Y',]] # Bottom , location X and Location Y are my column labels
df3 = df2.pivot('Location_X','Location_Y','Bottom') # create pivot table for results
plt.figure(figsize=(15,15))
pivot_table = df3
plt.xlabel('X',size = 10)
plt.ylabel('Y',size = 10)
plt.title('btm CD',size = 10)
sns.heatmap(pivot_table, annot=False, fmt=".1f", linewidths = 0.5, square = True, cmap = 'RdYlBu', vmin=2900, vmax = 3500)
plt.show()
In my data consist of 77 rows and 77 columns, of which only 651 have data, the others empty coordinates are indicated as None in the dataframe
在我的数据由 77 行和 77 列组成,其中只有 651 有数据,其他空坐标在数据框中表示为无
Is there a limitation of how many data can seaborn heatmap can plot?
seaborn 热图可以绘制多少数据是否有限制?
I am not sure why am i getting the above error, I have written it to a csv file and it turns out alright.
我不确定为什么会出现上述错误,我已将其写入 csv 文件,结果没问题。
in addition, I have try to replace the values to '0' and empty string but it still return the Typeerror
此外,我尝试将值替换为 '0' 和空字符串,但它仍然返回 Typeerror
回答by xiaxio
This error happens when you are trying to draw a graph with seaborn, and some of the values are not numeric.
当您尝试使用 seaborn 绘制图形并且某些值不是数字时,会发生此错误。
seaborn tries to coerce all data into numbers, but sometimes this error arises. The solution is that you make sure that you don't have NaNs or strings in your data.
seaborn 尝试将所有数据强制转换为数字,但有时会出现此错误。解决方案是确保数据中没有 NaN 或字符串。
Try using df.astype(float) before calling the seaborn graph library. Make sure that you also don't have any NaNs.
在调用 seaborn 图形库之前尝试使用 df.astype(float)。确保您也没有任何 NaN。
回答by marcotama
To add to @xiaxio's answer, if you are calling this programmatically and want to check whether the data is of a numeric type, you can use if not pd.to_numeric(df[some_column], errors='coerce').isna().any(): return
or something like that.
要添加到@xiaxio 的答案中,如果您以编程方式调用它并想检查数据是否为数字类型,则可以使用if not pd.to_numeric(df[some_column], errors='coerce').isna().any(): return
或类似的方法。
回答by Jacques MALAPRADE
In my case the DataFrame had no Nan
values but the data type was of type object
. What worked for me was to coerce the data to float using df = df.astype(float)
. Plotting using seaborn heatmap was then possible.
在我的例子中,DataFrame 没有Nan
值,但数据类型是 type object
。对我有用的是使用df = df.astype(float)
. 然后可以使用 seaborn 热图进行绘图。