How to evaluate and add string to numpy array element
<p>Have this piece of code that I am trying to optimize.
It uses list comprehensions and works. </p>

<pre><code>series1 = np.asarray(range(10)).astype(float)
series2 = series1[::-1]

ntup = zip(series1,series2)
[['', 't:'+str(series2)][series1 &gt; series2] for series1,series2 in ntup ]
 #['', '', '', '', '', 't:4.0', 't:3.0', 't:2.0', 't:1.0', 't:0.0']
</code></pre>

<p>Trying to use <code>np.where()</code> here. Is there a solution with <code>numpy</code>. (Without series being consumed)</p>

<pre><code>series1 = np.asarray(range(10)).astype(float)
series2 = series1[::-1]  

np.where(series1 &gt;  series2 ,'t:'+ str(series2),'' )
</code></pre>

<p>The results is this: </p>

<pre><code>array(['', '', '', '', '', 't:[ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.]',
       't:[ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.]',
       't:[ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.]',
       't:[ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.]',
       't:[ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.]'], 
      dtype='|S43')
</code></pre>

<p>This works for me. Fully vectorized.</p>

<pre><code>import numpy as np
series1 = np.arange(10)
series2 = series1[::-1]
empties = np.repeat('', series1.shape[0])
ts = np.repeat('t:', series1.shape[0])
s2str = series2.astype(np.str)
m = np.vstack([empties, np.core.defchararray.add(ts, s2str)])
cmp = np.int64(series1 &gt; series2)
idx = np.arange(m.shape[1])
res = m[cmp, idx]
print res 
</code></pre>

python string performance numpy