Numpy array trims string values
<p>Here is the code I am trying to execute</p>

<pre><code>matrix = []
sample = [10,10,'mike','']
for i in range(10):
    r = [sample] * 3
    matrix.append(r)

matrix = np.array(matrix)
matrix[1][1] = [123,123,'james', 'sdfsdfsdf werwerwer sdfsdfsd werwerwer sdfsdfsdf']
print matrix[1][1]
</code></pre>

<p>and here is what I am getting</p>

<pre><code>['123' '123' 'james' 'sdfsdfsdf w']
</code></pre>

<p>so basically the text is trimmed for some reason. Has anyone seen it before?</p>

<p>If you do not specify the <code>dtype</code> when you are converting your list to an array, it will use default behavior. In your case, you are mixing int's and strings, so it will default to unicode &lt;11:</p>

<pre><code>&gt;&gt;&gt; np.array([1,2,'a'])
array(['1', '2', 'a'],
dtype='&lt;U11')
</code></pre>

<p>When you try to add a new element with a length more than 11, it will truncate to the dtype:</p>

<pre><code>&gt;&gt;&gt; x = np.array([1,2,'a'])
&gt;&gt;&gt; x[2] = 'abcdefghijklmnopqrstuvwxyz'
&gt;&gt;&gt; x
array(['1', '2', 'abcdefghijk'],
      dtype='&lt;U11')
</code></pre>

<p>You can resolve this by specifying a higher <code>dtype</code> when you create the array:</p>

<pre><code>&gt;&gt;&gt; x = np.array([1,2,'a'], '&lt;U50')
&gt;&gt;&gt; x[2] = 'abcdefhijkmnopqrstuvwxyz'
&gt;&gt;&gt; x
array(['1', '2', 'abcdefhijkmnopqrstuvwxyz'],
      dtype='&lt;U50')
</code></pre>

python numpy