Fixing Python Pandas Error – Series objects are mutable and cannot be hashed

Python Pandas Error – Series objects are mutable and cannot be hashed is an error which occurs when you use a mutable object as a key in a dictionary which should not be done. There are many scenarios where the error might occur.

In this article I am going to show you why this error is happening and how you can solve this error. I am also going to share with you other possible solutions which have worked for many developers who suffered from this error.

Exploring the Python Pandas Error – Series objects are mutable and cannot be hashed

This is an error which occurs when you use a mutable object as a key in a dictionary which should not be done. There are many scenarios where the error might occur.

Please double check so you can avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
Series objects are mutable and cannot be hashed
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

The only fix that has solved the problem for me

I have already explained why the error takes place, but if you pay attention, what does the error message say ?

It says, series objects are mutable and cannot be hashed. When did we try to hash the object? This depends on your particular case but in most cases the hashing happens when you use a mutable object as a key in a dictionary.

The fix is simple, you should use immutable objects as keys in a dictionary instead of using mutable objects.

Its worth to note that, this might happen by accident, for example. The example bellow will generate the error and it is a complete accident.

                                                                       #
stores = no_headers.iloc[1:,[1]]
stores[i]
# now, you have a series object with a single value and it cannot be hashed, all of this was not the intention in the first place
                                                                       #

The solution is to use the line of code bellow instead of the first line of code above

                                                                       #
stores = no_headers.iloc[1:,1]
                                                                       #

By using no_headers.iloc[1:,1] instead of using no_headers.iloc[1:,[1]] you are creating the series directly which means the error will be avoided.

The fix above should be enough to cover the error in most environments, I wish you good luck with your projects.

Summing-up : 

This is the end of our article, I hope the solution we offered solved your issue, If you like our effort and explanations and want to support our team, please consider donating to our Kofi account above, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/