Question

[Solved] TypeError: unhashable type: ‘numpy.ndarray’ when trying to plot a DataFrame

I am new to Python and trying to create a plot graph from a DataFrame.

I used the following piece of code:

predictions= list() 
for the in range (10):
    predicted= StartARIMAForecasting(RegistrationRates, 5,1,0) 
    predictions.append(predicted)
    RegistrationRates.append(predicted)

data = {'Year':['2016','2017','2018','2019','2020','2021','2022','2023','2024','2025'], 'Registration Rate':predictions}
resultdf = pd.DataFrame(data)
print(resultdf)

plt.xlabel('Year')
plt.ylabel('Registration Rate')
plt.plot(resultdf)

Following output is seen:

0  2016   [50.68501406476124]
1  2017   [52.41297372600995]
2  2018    [54.0703599343735]
3  2019   [53.58327982434545]
4  2020  [55.647237533704754]
5  2021  [54.398197822219714]
6  2022   [55.06459335430334]
7  2023   [56.00171430250292]
8  2024   [55.70449088032122]
9  2025    [57.7127557392168]

but blank graph is plotted with following error:
TypeError: unhashable type: ‘numpy.ndarray’

Full stack-trace is provided below:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-1d843e3f6a23> in <module>
     58 plt.xlabel('Year')
     59 plt.ylabel('Registration Rate')
---> 60 plt.plot(resultdf)
     61 
     62 root.mainloop()

~Anaconda3libsite-packagesmatplotlibpyplot.py in plot(*args, **kwargs)
   3356                       mplDeprecation)
   3357     try:
-> 3358         ret = ax.plot(*args, **kwargs)
   3359     finally:
   3360         ax._hold = washold

~Anaconda3libsite-packagesmatplotlib__init__.py in inner(ax, *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

~Anaconda3libsite-packagesmatplotlibaxes_axes.py in plot(self, *args, **kwargs)
   1525         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
   1526 
-> 1527         for line in self._get_lines(*args, **kwargs):
   1528             self.add_line(line)
   1529             lines.append(line)

~Anaconda3libsite-packagesmatplotlibaxes_base.py in _grab_next_args(self, *args, **kwargs)
    404                 this += args[0],
    405                 args = args[1:]
--> 406             for seg in self._plot_args(this, kwargs):
    407                 yield seg
    408 

~Anaconda3libsite-packagesmatplotlibaxes_base.py in _plot_args(self, tup, kwargs)
    381             x, y = index_of(tup[-1])
    382 
--> 383         x, y = self._xy_from_xy(x, y)
    384 
    385         if self.command == 'plot':

~Anaconda3libsite-packagesmatplotlibaxes_base.py in _xy_from_xy(self, x, y)
    214         if self.axes.xaxis is not None and self.axes.yaxis is not None:
    215             bx = self.axes.xaxis.update_units(x)
--> 216             by = self.axes.yaxis.update_units(y)
    217 
    218             if self.command != 'plot':

~Anaconda3libsite-packagesmatplotlibaxis.py in update_units(self, data)
   1467         neednew = self.converter != converter
   1468         self.converter = converter
-> 1469         default = self.converter.default_units(data, self)
   1470         if default is not None and self.units is None:
   1471             self.set_units(default)

~Anaconda3libsite-packagesmatplotlibcategory.py in default_units(data, axis)
    113         # default_units->axis_info->convert
    114         if axis.units is None:
--> 115             axis.set_units(UnitData(data))
    116         else:
    117             axis.units.update(data)

~Anaconda3libsite-packagesmatplotlibcategory.py in __init__(self, data)
    180         self._counter = itertools.count(start=0)
    181         if data is not None:
--> 182             self.update(data)
    183 
    184     def update(self, data):

~Anaconda3libsite-packagesmatplotlibcategory.py in update(self, data)
    197         data = np.atleast_1d(np.array(data, dtype=object))
    198 
--> 199         for val in OrderedDict.fromkeys(data):
    200             if not isinstance(val, VALID_TYPES):
    201                 raise TypeError("{val!r} is not a string".format(val=val))

TypeError: unhashable type: 'numpy.ndarray'
Enquirer: GAMA

||

Solution #1:

If you check the type of your column 'Registration Rate', you will see that it’s type of numpy.ndarray as shown in the error.

type(resultdf['Registration Rate'][0])

So, maybe modify your predictions creation to make it a single element?

predictions= list() 
for the in range (10):
    predicted= StartARIMAForecasting(RegistrationRates, 5,1,0) 
    # predicted is a numpy.ndarray, len = 1
    p = predicted[0]
    predictions.append(p)

Then run your code a gain.

Respondent: hunzter

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy