2

I want to put numerics and strings into the same numpy array. However, I very rarely (difficult to replicate, but sometimes) run into an error where the numeric to string conversion results in a value that cannot back-translate into a decimal (ie, I get "9.8267567e", as opposed to "9.8267567e-5" in the array). This is causing problems after writing files. Here is an example of what I am doing (though on a much smaller scale):

import numpy as np
x = np.array(.94749128494582)
y = np.array(x, dtype='|S100')

My understanding is that this should allow 100 string characters, but sometimes I am seeing a cut-off after ~10. Is there another type that I should be assigning, or a way to limit the number of characters in my array (x)?

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
mike
  • 22,931
  • 31
  • 77
  • 100
  • Why are you using a string array for this? Also, if you want to put strings and floats into the same array, `numpy` is not what you want. You can do it through object arrays, but you loose the memory-effiency that is the entire point of numpy... Why not just use a list? – Joe Kington Jan 08 '12 at 15:47

1 Answers1

1

First of all, x = np.array(.94749128494582) may not be doing what you think because the argument passed into np.array should be some kind of sequence or something with the array interface. Perhaps you meant x = np.array([.94749128494582])?

Now, as for preserving the strings properly, you could solve this by using

y = np.array(x, dtype=object)

However, as Joe has mentioned in his comment, it's not very numpythonic and you may as well be using plain old python lists.

I would recommend to examine carefully why you seem to have this requirement to hold strings and numbers in the same array, it smells to me like you might have inappropriate data structures set up and could benefit from redesigning/refactoring. numpy arrays are for fast numerical operations, they are not really suited to be used for string manipulations or as some kind of storage/database.

wim
  • 338,267
  • 99
  • 616
  • 750