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>We can use a vectorized approach based on </p>

<ul>
<li><p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.core.defchararray.add.html" rel="nofollow"><code>np.core.defchararray.add</code></a> for the string appending of <code>'t:'</code> with the valid strings, and</p></li>
<li><p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html" rel="nofollow"><code>np.where</code></a> to choose based on the conditional statement and perform the appending or just use the default value of an empty string. </p></li>
</ul>

<p>So, we would have an implementation like so -</p>

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

<hr>

<p><strong>Boost it-up!</strong></p>

<p>We can use the appending with <code>np.core.defchararray.add</code> on the valid elements based on the mask of <code>series1&gt;series2</code> to boost up the performance further after initializing an array with the default empty strings and then assigning only the valid values into it. </p>

<p>So, the modified version would look something like this -</p>

<pre><code>mask = series1&gt;series2
out = np.full(series1.size,'',dtype='U34')
out[mask] = np.core.defchararray.add('t:',series2[mask].astype(str))
</code></pre>

<hr>

<p><strong>Runtime test</strong></p>

<p>Vectorized versions as functions :</p>

<pre><code>def vectorized_app1(series1,series2):
    mask = series1&gt;series2
    return np.where(mask,np.core.defchararray.add('t:',series2.astype(str)),'')

def vectorized_app2(series1,series2):
    mask = series1&gt;series2
    out = np.full(series1.size,'',dtype='U34')
    out[mask] = np.core.defchararray.add('t:',series2[mask].astype(str))
    return out
</code></pre>

<p>Timings on a bigger dataset -</p>

<pre><code>In [283]: # Setup input arrays
     ...: series1 = np.asarray(range(10000)).astype(float)
     ...: series2 = series1[::-1]
     ...: 

In [284]: %timeit [['', 't:'+str(s2)][s1 &gt; s2] for s1,s2 in zip(series1, series2)]
10 loops, best of 3: 32.1 ms per loop # OP/@hpaulj's soln

In [285]: %timeit vectorized_app1(series1,series2)
10 loops, best of 3: 20.5 ms per loop

In [286]: %timeit vectorized_app2(series1,series2)
100 loops, best of 3: 10.4 ms per loop
</code></pre>

<p>As noted by <a href="http://stackoverflow.com/questions/39256365/how-to-evalute-and-add-string-to-numpy-array-element#comment65850443_39256849"><code>OP in comments</code></a>, that we can probably play around with the dtype for <code>series2</code> before appending. So, I used <code>U32</code> there to keep the output dtype same as with <code>str</code> dtype, i.e. <code>series2.astype('U32')</code> inside the <code>np.core.defchararray.add</code> call. The new timings for the vectorized approaches were -</p>

<pre><code>In [290]: %timeit vectorized_app1(series1,series2)
10 loops, best of 3: 20.1 ms per loop

In [291]: %timeit vectorized_app2(series1,series2)
100 loops, best of 3: 10.1 ms per loop
</code></pre>

<p>So, there's some further marginal improvement there!</p>

python string performance numpy