Python: Why can't I assign new value to an array in this case?
<pre><code>import numpy as np

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

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

<pre><code>data[0,0]
</code></pre>

<blockquote>
  <p>'Height'</p>
</blockquote>

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

<pre><code>data[0,0] = "Height" + "_1"
</code></pre>

<p>result:</p>

<pre><code>data[0,0]
</code></pre>

<blockquote>
  <p>'Height'</p>
</blockquote>

<p>But if I replace it with some characters other than <strong>"Height"</strong>, it works.</p>

<pre><code>data[0,0] = "str" + "_1"
</code></pre>

<p>Result:</p>

<pre><code>data[0,0]
</code></pre>

<blockquote>
  <p>'str_1'</p>
</blockquote>

<p>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.</p>

<p>The problem is your array is of <code>dtype('&lt;U6')</code></p>

<pre><code>&gt;&gt;&gt; data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53']])
&gt;&gt;&gt; data.dtype
dtype('&lt;U6')
&gt;&gt;&gt; 
</code></pre>

<p>It will automatically truncate:</p>

<pre><code>&gt;&gt;&gt; data[0,0] = "123456789"
&gt;&gt;&gt; data
array([['123456', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], 
      dtype='&lt;U6')
&gt;&gt;&gt; 
</code></pre>

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

<p>Alternatively, you can specify a longer string type:</p>

<pre><code>&gt;&gt;&gt; data
array([['Height', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], 
      dtype='&lt;U20')
&gt;&gt;&gt; data[0,0]='Height_1'
&gt;&gt;&gt; data
array([['Height_1', 'Weight'],
       ['165', '48'],
       ['168', '50'],
       ['173', '53']], 
      dtype='&lt;U20')
&gt;&gt;&gt; 
</code></pre>

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

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

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

<pre><code>&gt;&gt;&gt; data = np.array([['Height', 'Weight'],['165', '48'],['168', '50'],['173', '53'], ['42','88']], dtype='S20')
&gt;&gt;&gt; data.nbytes
200
&gt;&gt;&gt; 
</code></pre>

python arrays numpy variable-assignment