. Advertisement .
..3..
. Advertisement .
..4..
In the process of completing my work, I encountered the following problem:
Series objects are mutable and cannot be hashed
Below is the code I ran:
cols = ['Gene type', 'Gene name', 'Disorder name']
no_headers = pd.read_csv('orphanet_infoneeded.csv', sep=',',header=None,names=cols)
gene_type = no_headers.iloc[1:,[0]]
gene_name = no_headers.iloc[1:,[1]]
disease_name = no_headers.iloc[1:,[2]]
query = 'Disease-causing germline mutation(s) in' ###add query as required
orph_dict = {}
for x in gene_name:
if gene_name[x] in orph_dict:
if gene_type[x] == query:
orph_dict[gene_name[x]]=+ 1
else:
pass
else:
orph_dict[gene_name[x]] = 0
What’s causing it, and how can it be resolved in the “typeerror: ‘series’ objects are mutable, thus they cannot be hashed“ in the python?
The cause:
This error happens because you passed a list of columns (single, but still a list). When you later do this:
Now you have a Series object with a single value, so can’t hash the Series.
Solution: You can resolve the error by creating Series from the start.
gene_name[x]
, short forgene_name[x]
, is a mutable object that cannot be hashed. Python must use the object’s hash value to create a dictionary key. This is why you get an error.Additional explanation
Mutable objects are objects whose value can be altered.
list
, for example, is a mutable item because you can add to it.int
, on the other hand, is immutable. You can’t modify it. If you do:a
‘s value is not changed. Instead, create a new object to pointa
to it.It is impossible to hash out mutable objects.
You should use immutable objects in your dictionary to solve your problem.
tuple
,string
andint
are examples.