Search code examples
pythonmatplotlibindexingtypeerrorhashable

Pandas Multi–Column Pie Plot with "TypeError: Int64Index.name must be a hashable type"


I am quite new to coding, and this seems to be some fundamental aspect of Python/Pandas/Matplotlib that I am not understanding. I am happy with general answers, but, for reference, here is my specific context:

top_100.index = top_100.Company
top_100 = top_100.dropna()
top_20 = top_100[(top_100.Rank > 10) & (top_100.Rank < 21)]
top_20 = top_20.sort_values('Rank', ascending = True)
top_20.index = top_20.Rank
plt.figure()
top_20.plot.pie(y = ['USA_Retail_Sales_million',\
                     'Worldwide_Retail_Sales_million'], subplots = True,\
                figsize = (16, 8), autopct = '%1.1f%%', legend = False)
plt.show()

The full error message reads:

runcell(65, 'C:/Users/Adam/Desktop/DSCI 200/Practice/Wk 3 Practice.py')
Traceback (most recent call last):

  File "C:\Users\Adam\Desktop\DSCI 200\Practice\Wk 3 Practice.py", line 359, in <module>
    top_20.plot.pie(y = ['USA_Retail_Sales_million',\

  File "C:\Users\Adam\anaconda3\lib\site-packages\pandas\plotting\_core.py", line 1528, in pie
    return self(kind="pie", **kwargs)

  File "C:\Users\Adam\anaconda3\lib\site-packages\pandas\plotting\_core.py", line 908, in __call__
    data.index.name = y

  File "C:\Users\Adam\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 1190, in name
    maybe_extract_name(value, None, type(self))

  File "C:\Users\Adam\anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 5665, in maybe_extract_name
    raise TypeError(f"{cls.__name__}.name must be a hashable type")

TypeError: Int64Index.name must be a hashable type

<Figure size 432x288 with 0 Axes>

For what it's worth, I don't have anywhere near 5600 lines of code.

Every time I run this code, I get the TypeError: Int64Index.name must be a hashable type. I have changed the index multiple time, but have come to realize that I don't think it's my index that is the issue; I am obviously asking it to change an immutable 'something'. I just don't know what that 'something' is, or how to otherwise approach the issue. Perhaps (obviously) I have a very nascent understanding of what Int64Index.name is and what I am asking it to do. I have certainly not ruled that my code is just plain wrong, but is seems correct to me - for what little that's worth.


Solution

  • I re–created the issue with the following minimum example (with Pandas 1.2.0):

    import pandas as pd
    import matplotlib.pyplot as plt
    
    # randomly made up data
    top_20 = pd.DataFrame(
        {'USA_Retail_Sales_million': [0.330, 4.87 , 5.97],
         'Worldwide_Retail_Sales_million': [2439.7, 6051.8, 6378.1]},
    )
    top_20.plot.pie(
        y=['USA_Retail_Sales_million', 'Worldwide_Retail_Sales_million'],
        subplots=True, figsize=(16, 8), autopct='%1.1f%%', legend=False
    )
    plt.show()
    

    The code above produces the same error. The pie function simply does not accept lists/tuples for the y argument. The correct way is to create a new DataFrame with the columns for plotting.

    import pandas as pd
    import matplotlib.pyplot as plt
    
    
    top_20 = pd.DataFrame(
        {'USA_Retail_Sales_million': [0.330, 4.87 , 5.97],
         'Worldwide_Retail_Sales_million': [2439.7, 6051.8, 6378.1]},
    )
    new_df = top_20[['USA_Retail_Sales_million', 'Worldwide_Retail_Sales_million']]
    new_df.plot.pie(subplots=True, figsize=(16, 8), autopct='%1.1f%%', legend=False)
    plt.show()