Converting int arrays to string arrays in numpy without truncation

115,634

Solution 1

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')

Solution 2

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.

Solution 3

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

import numpy as np

a = np.array([0,33,4444522])

res = a.astype(str)

print(res)

array(['0', '33', '4444522'], 
      dtype='<U11')

Solution 4

You can find the smallest sufficient width like so:

In [3]: max(len(str(x)) for x in [0,33,4444522])
Out[3]: 7

Alternatively, just construct the ndarray from a list of strings:

In [7]: np.array([str(x) for x in [0,33,4444522]])
Out[7]: 
array(['0', '33', '4444522'], 
      dtype='|S7')

or, using map():

In [8]: np.array(map(str, [0,33,4444522]))
Out[8]: 
array(['0', '33', '4444522'], 
      dtype='|S7')
Share:
115,634
Dave31415
Author by

Dave31415

Updated on July 09, 2022

Comments

  • Dave31415
    Dave31415 almost 2 years

    Trying to convert int arrays to string arrays in numpy

    In [66]: a=array([0,33,4444522])
    In [67]: a.astype(str)
    Out[67]: 
    array(['0', '3', '4'], 
          dtype='|S1')
    

    Not what I intended

    In [68]: a.astype('S10')
    Out[68]: 
    array(['0', '33', '4444522'], 
          dtype='|S10')
    

    This works but I had to know 10 was big enough to hold my longest string. Is there a way of doing this easily without knowing ahead of time what size string you need? It seems a little dangerous that it just quietly truncates your string without throwing an error.

    • Raketenolli
      Raketenolli about 6 years
      For visitors from 2018 and beyond: a.astype(str) will now work exactly as desired.
  • Dave31415
    Dave31415 about 12 years
    Thanks. I guess I need to become more acquainted with map.
  • Joel Cornett
    Joel Cornett about 12 years
    For large arrays, map() is a better option than a list comprehension because it pushes the execution of the code into C.
  • jorgeca
    jorgeca about 12 years
    @JoelCornett Sometimes map can be slightly faster (in this example it's about 3% faster than using a list comprehension for me), but this isn't always the case, and list comprehensions are considered more pythonic. See stackoverflow.com/a/1247490/1191119
  • Joel Cornett
    Joel Cornett about 12 years
    @jorgeca: Absolutely, it isn't always the case. Incidentally, as I was doing the research, I came upon this enlightening article by Guido.
  • jorgeca
    jorgeca about 12 years
    @JoelCornett Definitely, that's a great read. I knew it but it's well worth revisiting.
  • drevicko
    drevicko almost 9 years
    @jorgeca the article seems to have moved/evaporated - any idea where it is now? I couldn't find it in this list of essays, nor searching list2str on python.org...
  • Mikhail V
    Mikhail V over 7 years
    Cool, but I can't find documentation for this, could you provide a link if possible?
  • jorgeca
    jorgeca over 7 years
    @MikhailV Sure, just added a link to the answer.
  • Stardust
    Stardust over 3 years
    list(map(str, [0,33,4444522]))
  • jorgeca
    jorgeca over 2 years
    @drevicko here's an updated link