Today, while using Python’s Seaborn to plot a heatmap (clustermap), I kept encountering this error. My data seemed perfectly fine, and a Google search didn’t yield any good solutions. After some exploration, I’m sharing the final solution here.


1. Generating the DataFrame

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from seaborn import clustermap
    import seaborn as sns; sns.set(color_codes=True)
    df = pd.DataFrame([["a","b","c","d","e","f"],[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8]],  columns=list('ABCDEF')).T
    df
    g = sns.clustermap(df.iloc[:,1:],cmap="PiYG")

After generating and transposing the DataFrame, a TypeError occurs: 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".

《TypeError: ufunc ‘isnan’ not supported for the input types solution》


2. Cause of the Error

This type of error arises because the DataFrame has been transposed, and the original DataFrame contained string columns. Just like in the example above, the first column contains strings (values ‘abcdef’). When transposed, all numerical values in the DataFrame are also converted to object types instead of float or int numerical types. Therefore, trying to plot a heatmap with character types naturally leads to an error.

Note that if the DataFrame originally contained only numerical types, this issue wouldn’t occur.


3. Solution

Knowing the cause makes the solution simple: convert the corresponding numerical columns in the transposed DataFrame back to numerical types. Here’s the code:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from seaborn import clustermap
    import seaborn as sns; sns.set(color_codes=True)
    df = pd.DataFrame([["a","b","c","d","e","f"],[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8]],  columns=list('ABCDEF')).T
    df[1] = pd.Series(df[1]).astype(float)
    df[2] = pd.Series(df[2]).astype(float)
    df[3] = pd.Series(df[3]).astype(float)
    g = sns.clustermap(df.iloc[:,1:],cmap="PiYG")

By using the Pandas Series type conversion function astype to convert the relevant columns to float type, you can then successfully plot the clustered heatmap.

《TypeError: ufunc ‘isnan’ not supported for the input types solution》