Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Ryan May
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Adam Mercer
On 08/10/2007, Ryan May [EMAIL PROTECTED] 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.e+00   0.e+00   0.e+00 ...,
   4.22233200e-23   3.86799900e-23   3.48452000e-23]

Cheers

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Robert Kern
Adam Mercer wrote:
 On 08/10/2007, Ryan May [EMAIL PROTECTED] 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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Charles R Harris
On 10/8/07, Adam Mercer [EMAIL PROTECTED] wrote:

 On 08/10/2007, Ryan May [EMAIL PROTECTED] 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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Adam Mercer
On 08/10/2007, Robert Kern [EMAIL PROTECTED] 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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Convert array type

2007-10-06 Thread Adam Mercer
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-06 Thread Gary Ruben
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)
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-06 Thread Adam Mercer
On 07/10/2007, Gary Ruben [EMAIL PROTECTED] wrote:

 Try using astype. This works:

 values = array(wavearray.split()).astype(float)

Thanks Gary, that does the trick.

Cheers

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion