On Jun 9, 2012, at 4:45 PM, josef.p...@gmail.com wrote: > Is there a way to convert an array to string elements in numpy, > without knowing the string length?
Not really. In the next release of NumPy you should be able to do. result = array(arr2, str) and it will determine the length of the string for you. For now, I would compute the max length via int(math.ceil(math.log10(np.max(arr2)))) -Travis > > >>>> arr2 = np.arange(8, 13) > >>>> arr2.astype(str) # bad > array(['8', '9', '1', '1', '1'], > dtype='|S1') > >>>> arr2.astype('S2') > array(['8', '9', '10', '11', '12'], > dtype='|S2') > >>>> map(str, arr2) > ['8', '9', '10', '11', '12'] > > >>>> arr3 = np.round(np.random.rand(5), 2) >>>> arr3 > array([ 0.51, 0.86, 0.15, 0.68, 0.59]) > >>>> arr3.astype(str) # bad > array(['0', '0', '0', '0', '0'], > dtype='|S1') > >>>> arr3.astype('S4') > array(['0.51', '0.86', '0.15', '0.68', '0.59'], > dtype='|S4') > >>>> map(str, arr3) > ['0.51', '0.86', '0.15', '0.68', '0.59'] > >>>> np.__version__ > '1.5.1' > > (from an issue in statsmodels) > > Thanks, > > Josef > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion