Jim Ou
Jim Ou

Reputation: 33

Python: Why can't I assign new value to an array in this case?

import numpy as np

data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']])
data[0,0] = data[0,0] + "_1"

data[0,0] is 'Height', and I want to replace it with 'Height_1'. But the code above doesn't work. It returned the result as:

data[0,0]

'Height'

The data[0,0] element remained the same. And if I replace it directly without referring to itself, it still doesn't work.

data[0,0] = "Height" + "_1"

result:

data[0,0]

'Height'

But if I replace it with some characters other than "Height", it works.

data[0,0] = "str" + "_1"

Result:

data[0,0]

'str_1'

I took this case to explain the problem I'm coming across. And in my work I have to refer to the array itself because I need to replace the elements which don't meet some requirements. Anyone have solutions on this? Thank you.

Upvotes: 3

Views: 571

Answers (2)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96287

The problem is your array is of dtype('<U6')

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']])
>>> data.dtype
dtype('<U6')
>>> 

It will automatically truncate:

>>> data[0,0] = "123456789"
>>> data
array([['123456', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], 
      dtype='<U6')
>>> 

You can always specify your dtype as 'object' when you create your array, but this removes a lot of the speed benefits of numpy to begin with.

Alternatively, you can specify a longer string type:

>>> data
array([['Height', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], 
      dtype='<U20')
>>> data[0,0]='Height_1'
>>> data
array([['Height_1', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], 
      dtype='<U20')
>>> 

But be careful, as if you set the limit too-long you will be wasting memory:

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U20')
>>> data.nbytes
800
>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='U6')
>>> data.nbytes
240

If you only need a limited amount of characters, consider using byte-strings (1/4th the memory requirement):

>>> data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='S20')
>>> data.nbytes
200
>>> 

Upvotes: 4

sramij
sramij

Reputation: 4925

specify an object type for your array, such as:

a = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']],dtype=object)

Then, a[0][0]+='_1' will do the trick, you will get:

array([['Height_1', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], dtype=object)

Upvotes: 3

Related Questions