How to Solve Python TypeError: Series objects are mutable and cannot be hashed

by | Programming, Python, Tips

Introduction

When working with pandas in Python, you might encounter the error:
TypeError: Series objects are mutable and cannot be hashed.
This error often occurs when you attempt to use a pandas Series as a key in a dictionary or try to perform operations that require an immutable, hashable type. Let’s dive into an example where this error can occur and explain how to fix it.

Example to Reproduce the Error

Imagine you’re trying to create a dictionary using pandas Series as keys:

import pandas as pd

# Create a simple dataframe
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35]}

df = pd.DataFrame(data)

# Attempt to use a pandas Series as a dictionary key
my_dict = {df['Name']: 'People'}  # Error occurs here

Error Message:

When you run the above code, you’ll get the following error:

TypeError: Series objects are mutable and cannot be hashed

or

TypeError: unhashable type: 'Series'

This happens because pandas Series are mutable objects, meaning they can change, and therefore, they cannot be used as dictionary keys, which require hashable (immutable) types.

Solution

To fix this error, you need to use immutable types like tuples, strings, or numbers as dictionary keys. One common solution is to convert the pandas Series to a tuple, which is hashable, before using it as a dictionary key.

Correct Approach:

Here’s how you can solve the issue by converting the Series to a tuple before using it as a dictionary key:

# Convert the Series to a tuple before using it as a dictionary key
my_dict = {tuple(df['Name']): 'People'}
print(my_dict)

Output:

{('Alice', 'Bob', 'Charlie'): 'People'}

This works because tuple(df['Name']) converts the mutable Series into an immutable tuple, making it valid to use as a dictionary key.

Alternatively, if you want to use individual elements of the Series as keys, you can iterate through the Series like this:

# Use individual values from the Series as dictionary keys
my_dict = {name: 'Person' for name in df['Name']}
print(my_dict)

Output:

{'Alice': 'Person', 'Bob': 'Person', 'Charlie': 'Person'}

Conclusion:

The TypeError: Series objects are mutable and cannot be hashed is common when you attempt to use pandas Series in ways that require immutability, like in dictionary keys. By converting the Series to an immutable type, such as a tuple, or using individual elements, you can quickly resolve this issue.

Congratulations on reading to the end of this tutorial!

For further reading on TypeErrors involving Pandas, go to the articles:

For further reading on Pandas, go to the article: Introduction to Pandas: A Complete Tutorial for Beginners.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee