Converting int arrays to string arrays in numpy without truncation Converting int arrays to string arrays in numpy without truncation numpy numpy

Converting int arrays to string arrays in numpy without truncation


Again, this can be solved in pure Python:

>>> map(str, [0,33,4444522])['0', '33', '4444522']

Or if you need to convert back and forth:

>>> a = np.array([0,33,4444522])>>> np.array(map(str, a))array(['0', '33', '4444522'],       dtype='|S7')


You can stay in numpy, doing

np.char.mod('%d', a)

This is twice faster than map or list comprehensions for 10 elements, four times faster for 100. This and other string operations are documented here.


Use arr.astype(str), as int to str conversion is now supported by numpy with the desired outcome:

import numpy as npa = np.array([0,33,4444522])res = a.astype(str)print(res)array(['0', '33', '4444522'],       dtype='<U11')