1

I've read that numpy arrays are hashable which means it is immutable but I'm able to change it's values so what does it exactly mean by being hashable?

c=pd.Series('a',index=range(6))
c
Out[276]: 
0    a
1    a
2    a
3    a
4    a
5    a
dtype: object

This doesn't give me error then why it gives error if I try to do the same with numpy array.

d=pd.Series(np.array(['a']),index=range(6))
2
  • 5
    "I've read that numpy arrays are hashable" where have you read this? Commented Aug 6, 2018 at 5:49
  • c fills all elements with the scalar string value. What's the error for d? That has significant information. Commented Aug 6, 2018 at 6:07

1 Answer 1

6

Contrary to what you have read, array are not hashable. You can test this with

import numpy as np,collections
isinstance(np.array(1), collections.Hashable)

or

{np.array(1):1}

This has nothing to do with the error you are getting:

d=pd.Series(np.array('a'),index=range(6))
ValueError: Wrong number of dimensions

the error is specific, and has nothing to do with hashes. The data frame is expecting at least something with 1 dimension, whereas the above has 0 dimensions. This is due to the fact it is getting an array - so it checks the dimension (as opposed to passing the string directly, where Pandas developers have chosen to implement as you have shown. TBH they could have chosen the same for a 0 dimension array).

So you could try:

d=pd.Series(np.array(('a',)),index=range(6))
ValueError: Wrong number of items passed 1, placement implies 6

The index value expects there to be a 6 in one dimension, so it fails. Finally

pd.Series(np.array(['a']*6),index=range(6))
0    a
1    a
2    a
3    a
4    a
5    a
dtype: object

works. So the DataFrame has no problem being initiated from an array, and this has nothing to do with hashability.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.