0

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Renzzy
  • 25
  • 1
  • 8
  • 1
    Show which line the error is happening at by editing the __full__ error message into your question. – DisappointedByUnaccountableMod Jan 30 '21 at 17:54
  • Is it happening on the line `top_100.index = top_100.Company`? If so, I suspect that `top_100.Company` is a list of objects of type `Company`? Can't say much without the error message and some context of your data. – sudeep Jan 30 '21 at 17:57
  • As requested , I have edited in the full error message. It seems to be occurring at line 5665? One the other hand, the code for the pie chart itself is written on lines 358 - 362. – Renzzy Jan 30 '21 at 18:01
  • In the pie method, y can only be a label or an index, not a list of labels. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.pie.html – Stefan Jan 30 '21 at 18:05

1 Answers1

0

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()
Yang Yushi
  • 725
  • 4
  • 20
  • This seems to make more sense, and I think I understand the changes you made and why you made them. That said, I changed my code, like you suggested, and now I get a new error message saying `TypeError: 'PlotAccessor' object is not subscriptable`. Code reads: `plt.figure() top_20.plot[['USA_Retail_Sales_million',\ 'Worldwide_Retail_Sales_million']].pie(subplots = True,\ figsize = (16, 8), autopct = '%1.1f%%', legend = False) plt.show()` ...and the error message flags this line: `top_20.plot[['USA_Retail_Sales_million',\` – Renzzy Jan 30 '21 at 18:20
  • Sorry my code is buggy. It should be `top_20[[ 'USA_Retail_Sales_million', 'Worldwide_Retail_Sales_million' ]].plot.pie(subplots=True, figsize=(16, 8), autopct='%1.1f%%', legend=False)`. It is good in the answer now btw – Yang Yushi Jan 30 '21 at 18:22
  • Even with the correction I am still getting the new `TypeError: 'PlotAccessor' object is not subscriptable` error... That's a new one to me. – Renzzy Jan 30 '21 at 18:30
  • If the variable `top_20` is a DataFrame, then the code should be fine. I am a bit confused. I edited the answer to match your variable names. I hope it does help – Yang Yushi Jan 30 '21 at 18:33
  • Oh, no, your code was perfect, but my fingers were decidedly not. I matched your code perfectly this time and got exactly the result I was looking for. The change makes perfect sense to me, and I completely understand the mistakes I was making before. Thank you very much! – Renzzy Jan 30 '21 at 18:57