
Hi I am fairly new to using numpy and am running into a problem regarding the type of an array. The array in question is created using the following code: values = array(wavearray.split()) where wavearray is a string containing a series of floats separated by white space, it appears that the individual elements of the values array are strings, where I need them to be floats. How can I ensure that the individual elements of the values array are floats, not strings? I have tried using values = array(wavearray.split(), dtype=float) but that results in the error: ValueError: setting an array element with a sequence. Cheers Adam

Try using astype. This works: values = array(wavearray.split()).astype(float) Gary R. Adam Mercer wrote:
values = array(wavearray.split())
where wavearray is a string containing a series of floats separated by white space, it appears that the individual elements of the values array are strings, where I need them to be floats. How can I ensure that the individual elements of the values array are floats, not strings?
I have tried using
values = array(wavearray.split(), dtype=float)

Gary Ruben wrote:
Try using astype. This works:
values = array(wavearray.split()).astype(float)
Why not use numpy.fromstring? fromstring(string, dtype=float, count=-1, sep='') Return a new 1d array initialized from the raw binary data in string. If count is positive, the new array will have count elements, otherwise its size is determined by the size of string. If sep is not empty then the string is interpreted in ASCII mode and converted to the desired number type using sep as the separator between elements (extra whitespace is ignored). Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma

On 08/10/2007, Ryan May <rmay@ou.edu> wrote:
Why not use numpy.fromstring?
because that results in the array being filled with gibberish values = numpy.fromstring(wavearray, dtype=float, count=-1, sep='') print values gives: [ 1.39804329e-076 1.30354290e-076 1.18295070e-076 ..., 5.45168074e-067 2.11101912e-052 6.58519056e-260] where using values = array(wavearray.split()).astype(float) print values results in the correct [ 0.00000000e+00 0.00000000e+00 0.00000000e+00 ..., 4.22233200e-23 3.86799900e-23 3.48452000e-23] Cheers Adam

Adam Mercer wrote:
On 08/10/2007, Ryan May <rmay@ou.edu> wrote:
Why not use numpy.fromstring?
because that results in the array being filled with gibberish
values = numpy.fromstring(wavearray, dtype=float, count=-1, sep='')
Use sep=' '. As the docstring says, if sep is empty, then the string is interpreted as binary data. If it is not empty, then the string is interpreted as ASCII. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

On 08/10/2007, Robert Kern <robert.kern@gmail.com> wrote:
Use sep=' '. As the docstring says, if sep is empty, then the string is interpreted as binary data. If it is not empty, then the string is interpreted as ASCII.
Thanks, got it the wrong way round. That works now. Cheers Adam

On 10/8/07, Adam Mercer <ramercer@gmail.com> wrote:
On 08/10/2007, Ryan May <rmay@ou.edu> wrote:
Why not use numpy.fromstring?
because that results in the array being filled with gibberish
values = numpy.fromstring(wavearray, dtype=float, count=-1, sep='') print values
You need to use values = numpy.fromstring(wavearray, dtype=float, count=-1, sep=' ') Note that sep is a blank (whitespace). This will convert ascii strings. Chuck
participants (5)
-
Adam Mercer
-
Charles R Harris
-
Gary Ruben
-
Robert Kern
-
Ryan May