The error “Series Objects Are Mutable and Cannot be Hashed” can be hard to understand if you aren’t aware of advanced data structures like hash tables. In this article, we will do our best to help you get the hang of them and how it is related to our issue.
Series Objects Are Mutable and Cannot be Hashed
Reproduce The Error
For instance, we have this DataFrame:
import pandas as pd df = pd.DataFrame( [ ['LearnShareIT', 'Quora', 'Reddit', 'Stack Overflow'], ['tutorials', 'Q&A', 'Forum', 'Q&A'] ] ) print(df)
Output:
0 1 2 3
0 LearnShareIT Quora Reddit Stack Overflow
1 tutorials Q&A Forum Q&A
And we write this code to get the ranking of each site using values stored in another dict:
myDict = { 'LearnShareIT': 1, 'Quora': 2, 'Reddit': 3, 'Stack Overflow': 4 } for x in df[:1]: key = df[:1][x] print(myDict.get(key))
However, we run into this error instead:
Series objects are mutable and cannot be hashed
To know why it happens, you must get an understanding of hashable objects in Python.
Hash Functions And Hash Tables
Hashing is a popular technique in computing used to identify an object from a larger group of similar objects. It makes use of hash functions and hash functions.
A hash table contains key-value pairs. In many cases, we can use the key to directly retrieve the corresponding value. However, this isn’t always feasible. A hash function is called on this key, producing a unique value for the key.
We can store this hash value alongside its value in the hash table, allowing us to access data more efficiently. With hash tables, we can reduce complex objects to simpler associative arrays.
Hashable Objects In Python
An object in Python is called hashable if it is associated with a constant hash value throughout its lifetime. These objects have a __hash__()
method, which returns this hash value.
We can use hashable objects as keys in dictionaries and members in sets. Those data structures need to use hash values under the hood to access and modify internal data.
The most important thing you need to remember is that not every Python object is hashable. Most immutable objects are and have a __hash__()
method:
As you can see, the hash function reduces Python strings to numbers.
Mutable containers (dictionaries and lists) are not hashable. You will run into when calling the __hash__()
method:
lists
>>> c = [1, 2, 3]
>>> c.__hash__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
dicts
>>> d = {'name': 'LearnShareIT', 'type': 'tutorials'}
>>> d.__hash__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Immutable containers like frozensets and tuples can be hashable or not, It depends on whether their elements are hashable.
Tuples with hashable elements
>>> e = (1, 2, 'LearnShareIT')
>>> e.__hash__()
-2023910400398544926
Tuples with unhashable elements
>>> f = (1, [2, 3])
>>> f.__hash__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Solution
Python requires you to use hashable objects as keys in a dictionary. This way, it can quickly loop up the values of those keys by using their hash values. Since we use Series (which are unhashable), Python returns the error instead.
We must convert it to hashable objects (like strings) first:
for x in df[:1]: key = df[:1][x][0] print(myDict.get(key))
Output:
1
2
3
4
Summary
The error “Series Objects Are Mutable and Cannot be Hashed” occurs when you use Series as keys to obtain values from a dictionary. Pandas Series aren’t hashable and can’t be used for such purposes.
Maybe you are interested in similar errors:
- SyntaxError: Missing parentheses in call to ‘print’ in Python
- Reindexing only valid with uniquely valued Index objects

My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python