Typeerror: 'series' objects are mutable, thus they cannot be hashed

1 3
calendar_today agoschedule4 min read

TypeError: 'Series' objects are mutable, thus they cannot be hashed is an error message that can occur when working with pandas, a popular data manipulation library for Python. This error message is raised when trying to use a pandas Series object (a one-dimensional array-like object) as a key in a dictionary or as an element in a set. In this article we are going to look at the idea behind hashing and why a mutable object cannot be hashed which will give raise to the error in consideration.


Brief note on hashing

In Python, a series object is a one-dimensional array-like object that can hold any data type. It is similar to a column in a spreadsheet or a dataframe in pandas. One of the key features of a series object is that it is mutable, meaning that its values can be changed. However, this also means that 'series' objects cannot be hashed.

Hashing is a process of converting an input of any length into a fixed-length output, known as a hash value. The hash value is a unique representation of the input, and it is often used for efficient data searching and indexing. However, since 'series' objects are mutable, they cannot be hashed as their values can change, making it impossible to generate a unique hash value.

One of the most popular uses of hashing is in data structures such as hash tables and hash maps. These data structures use a hash function to convert a key (such as a name or a product ID) into a unique index in an array. This allows for quick and efficient lookup of the corresponding value (such as a price or a product description).

Tip Hashing algorithms come in a variety of forms, such as MD5, SHA-1, and SHA-256, each with their own unique properties and trade-offs. For example, while MD5 is fast and widely supported, it is not considered as secure as the SHA-256 algorithm.

Why the error?

The reason for the error Typeerror: 'series' objects are mutable, thus they cannot be hashed is that in Python, dictionaries and sets use a technique called hashing to quickly look up values based on their keys. Hashing involves taking an input (in this case, the key of a dictionary or set), running it through a mathematical function, and producing a fixed-length output (the "hash value"). The hash value is then used to quickly locate the corresponding value in the dictionary or set.

Here is an example of trying to hash a 'series' object:


import pandas as pd Creating a 'series' object s = pd.Series([1, 2, 3]) Attempting to hash the 'series' object hash(s)

The above code will result in the following error:


TypeError: unhashable type: 'Series'

However, for a data structure to be usable as a key, it must meet certain requirements. One of the most important of these is that the key must be immutable, meaning that its value cannot be changed after it is created. This is because the hash value of a key is calculated based on its value, so if the key's value changes, its hash value would also change. This would make it impossible to locate the key in the dictionary or set.

Pandas Series objects are mutable, meaning that their values can be changed after they are created. This makes them incompatible with dictionaries and sets, as their hash value would change if the values in the Series were modified.

However, if a 'series' object needs to be used in a hash table, it must first be converted to an immutable object such as a tuple or a frozen set.

A walk around

You can convert the Series to an immutable object such as a tuple or a string using the tolist() method or use pd.Index class to create an immutable index.

Here is an example of converting a 'series' object to a tuple:


import pandas as pd

# Creating a 'series' object
s = pd.Series([1, 2, 3])

# Converting the 'series' object to a tuple
s = tuple(s)

# Hashing the tuple
print(hash(s))

The above code will output a hash value for the tuple, allowing it to be used in a hash table.

Note It is important to keep in mind that this conversion may affect the performance of the code, as the conversion process may take a longer time compared to using a mutable object.

Conclusion

In conclusion, the TypeError: 'Series' objects are mutable, thus they cannot be hashed is raised when trying to use a pandas Series object as a key in a dictionary or as an element in a set because they are mutable objects. To fix this error, you can convert the Series to an immutable object such as a tuple or a string or use pd.Index class to create an immutable index.

References

Pandas documentation on Series: https://pandas.pydata.org/pandas-docs/stable/user_guide/dsintro.html#series
Python documentation on dictionaries: https://docs.python.org/3/tutorial/datastructures.html#dictionaries
Python documentation on sets: https://docs.python.org/3/tutorial/datastructures.html#sets
Understanding Hash Tables: https://en.wikipedia.org/wiki/Hash_table
How to convert pandas Series to a NumPy array: https://datatofish.com/convert-pandas-dataframe-to-numpy-array/

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

Understanding and debugging - TypeError :unhashable type: dict

Vanessa Paul - Nov 23, 2025

I Wrote a Script to Fix Audible's Unreadable PDF Filenames

snapsynapseverified - Apr 20

Understanding the "TypeError: cannot convert the series to <class 'float'>" in Pandas

Danny Jay - Jan 14

Why Prompt Engineering Is Just an Expensive Way to Be Incompetent

Karol Modelski - May 21
chevron_left
132 Points4 Badges
1Posts
3Comments
1Connections
I’m a software developer who enjoys building things, solving problems, and learning new technologies... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!