Python throws the error, ‘series’ objects are mutable, thus they cannot be hashed when a mutable object like array is used as dictionary key for some other object.
According to Python glossary –
Most of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not. Immutable objects include numbers, strings and tuples. Such an object cannot be altered.
But why hashes are so important?
Actually, Python uses hashvalues to refer a dictionary item, because hash values do not change for a given value. If the reference key is immutable object like numbers, strings, tuples etc. then we are confident that their hashes will remain the same. For example –
a = 4
a = 9
Here, the value of a is not altered, else it is recreated. That’s why it is immutable. But take this example –
myArray = []
myArray[0] = ‘Captain America’
myArray[0] = ‘Ironman’
Here, myArray remained the same for both assignments. When we change any value of list, all other values remain untouched. So, the list is not recreated else it is altered. That’s why it is mutable and can’t be used as hashes.
This code will throw error, ‘series’ objects are mutable –
superHero = [0, 1, [2]] superHeroToColor = [ 'ironman', 'hulk', 'thor' ] for x in superHero: print(superHeroToColor[x])
It is because one of the element of superHero
array is list.