. Advertisement .
..3..
. Advertisement .
..4..
The “typeerror: ‘ series’ objects are mutable, thus they cannot be hashed” often results from the hashing of mutable objects. Here, hashing is only available with immutable objects.
There are various causes and methods to fix this error in depth. This tutorial will introduce to you all the necessary information.
Why Do You Get The Error?
Here are some leading causes of this typeerror:
- Mutable objects or series cannot be hashed.
Mutable objects cannot be used when you work in a list in Python. So if you try to hash them, you will face the problem. The same case is for the series in DataFrames.
Passing various columns gives you a series objects of the same value. Thus, you cannot hash it.
- Pandas dataframe
When your code creates a circular reference, it will accidentally change values and affect the selection. It can be put down to a missing label-based of some unsuitable methods.
- Use of temp in dataframe
In DataFrame, the temp[index, ‘Any name’] function uses column selection using boolean and label. This way, the process will not work properly and cause the error.
- Use “Fillna” and “Apply”
When taking data from a column from Pandas, you tend to evaluate the data with the textstat module and note the results before using “fillna”. This can also be a reason for the error.
- Use reserved methods or PD.series objects
Reserved methods like .cat are among the potential causes for the typeerror. Plus, if there are some pd.objects in your series, which is unhashable, the problem occurs as well.
How To Solve TypeError: ‘Series’ Objects Are Mutable, Thus They Cannot Be Hashed
Method 1: Use A Hash Protocol
You can make your list hashable by executing _hash_protocol. The code to develop the list’s sub-classes and call a hash protocol is very straightforward:
class hashable_list(list):
def__init__(selc,*args):
if (len(args) == 1 and isinstance (args[0], iterable):
argos = argos[0].super().__init__(args)
def __hash__(self):
return hash (x for x in self)
Method 2: Use The Series Objects
This second method is applicable for unique field values such as ID numbers. Here, you use the series objects as a field instead of a key.
hash(ser2[“id”])
#142930
dic1={ser2[“id”]: “cat”}
print(dic1)
Method 3: Use Tuple
Tuple can be used to convert mutable objects into immutable ones. Here is how it works:
tup=tuple(ser2)
hash(tup)
Get the agent’s name:
list(dic2.keys())
Dic2 {tup: “Cat”}
print(dic2)
Immutable and hashable results:
Employer = namedtuple(‘employer’, dic2)
Em1 = employer(**dic2)
print(em1)
Employer(id=178262, name= ‘Mark’, job_title = ‘teacher’, salary =10000)
You can also create another dictionary to create other key fields by name:
for key in dic3:
print(key.name)
print(dic3[key])
Method 4: Use Series To Create A Class
If you use mutable series, use it to create and add the hash function to a new class. This way, when the hash changes, the objects will stop functioning:
Class MySeries(pd.Series):
Def __hash__(self):
Return hash(tuple(self))
Myser = MySeries([768463, “Mark”, “teacher”, “10000”), index=[“id”, “name”, “job_title”, “salary”])
hash(myser)
dic={myser: “Cat”}
print(dic)
Conclusion
The post discusses the potential causes and possible methods to deal with the typeerror: ‘series objects are mutable, thus they cannot be hashed error.
Leave a comment