5

So, this should be a really straightforward thing but for whatever reason, nothing I'm doing to convert an array of strings to an array of floats is working.

I have a two column array, like so:

Name    Value
Bob     4.56
Sam     5.22
Amy     1.22

I try this:

for row in myarray[1:,]:
     row[1]=float(row[1])

And this:

for row in myarray[1:,]:
    row[1]=row[1].astype(1)

And this:

myarray[1:,1] = map(float, myarray[1:,1])

And they all seem to do something, but when I double check:

type(myarray[9,1])

I get

<type> 'numpy.string_'>
f4lco
  • 3,728
  • 5
  • 28
  • 53
Olga Mu
  • 908
  • 2
  • 12
  • 23
  • http://stackoverflow.com/questions/9949427/how-to-change-the-dtype-of-a-numpy-recarray – Joran Beasley Mar 31 '13 at 22:43
  • 2
    Aside: if you're working with mixed-type data, it might be worth your time to look at [pandas](http://pandas.pydata.org). It already incorporates a lot of the tools you'd otherwise need to reimplement in order to get pure `numpy` to do the sort of ops people usually perform on names-and-numbers datasets. – DSM Mar 31 '13 at 22:54

2 Answers2

7

Numpy arrays must have one dtype unless it is structured. Since you have some strings in the array, they must all be strings.

If you wish to have a complex dtype, you may do so:

import numpy as np
a = np.array([('Bob','4.56'), ('Sam','5.22'),('Amy', '1.22')], dtype = [('name','S3'),('val',float)])

Note that a is now a 1d structured array, where each element is a tuple of type dtype.

You can access the values using their field name:

In [21]: a = np.array([('Bob','4.56'), ('Sam','5.22'),('Amy', '1.22')],
    ...:         dtype = [('name','S3'),('val',float)])

In [22]: a
Out[22]: 
array([('Bob', 4.56), ('Sam', 5.22), ('Amy', 1.22)], 
      dtype=[('name', 'S3'), ('val', '<f8')])

In [23]: a['val']
Out[23]: array([ 4.56,  5.22,  1.22])

In [24]: a['name']
Out[24]: 
array(['Bob', 'Sam', 'Amy'], 
      dtype='|S3')
askewchan
  • 45,161
  • 17
  • 118
  • 134
0

The type of the objects in a numpy array is determined at the initialsation of that array. If you want to change that later, you must cast the array, not the objects within that array.

myNewArray = myArray.asType(float)

Note: Upcasting is possible, for downcasting you need the astype method. For further information see:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html http://docs.scipy.org/doc/numpy/reference/generated/numpy.chararray.astype.html

cbare
  • 12,060
  • 8
  • 56
  • 63
Benjamin
  • 609
  • 3
  • 8
  • 2
    But the array has strings like `'Bob'` which cannot be converted into a float, so it will give `ValueError: could not convert string to float: 'Bob'` – askewchan Mar 31 '13 at 22:51