title: How to convert numpy object array into str/unicode array?
body: <p><strong>Update:</strong>  In lastest version of numpy (e.g., v1.8.1), this is no longer a issue. All the methods mentioned here now work as excepted.</p>
  
  <p><strong>Original question:</strong>  Using object dtype to store string array is convenient sometimes, especially when one needs to modify the content of a large array without prior knowledge about the maximum length of the strings, e.g., </p>
  
  <pre><code>&gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; a = np.array([u'abc', u'12345'], dtype=object)
  </code></pre>
  
  <p>At some point, one might want to convert the dtype back to unicode or str. However, simple conversion will truncate the string at length 4 or 1 (why?), e.g., </p>
  
  <pre><code>&gt;&gt;&gt; b = np.array(a, dtype=unicode)
  &gt;&gt;&gt; b
  array([u'abc', u'1234'], dtype='&lt;U4')
  &gt;&gt;&gt; c = a.astype(unicode)
  &gt;&gt;&gt; c
  array([u'a', u'1'], dtype='&lt;U1')
  </code></pre>
  
  <p>Of course, one can always iterate over the entire array explicitly to determine the max length, </p>
  
  <pre><code>&gt;&gt;&gt; d = np.array(a, dtype='&lt;U{0}'.format(np.max([len(x) for x in a])))
  array([u'abc', u'12345'], dtype='&lt;U5')
  </code></pre>
  
  <p>Yet, this is a little bit awkward in my opinion.  Is there a better way to do this?</p>
  
  <p><strong>Edit to add:</strong>  According to this <a href="https://stackoverflow.com/questions/1582525/longest-string-in-numpy-object-array">closely related question</a>, </p>
  
  <pre><code>&gt;&gt;&gt; len(max(a, key=len))
  </code></pre>
  
  <p>is another way to find out the longest string length, and this step seems to be unavoidable...</p>