[SciPy-user] casting ndarray object to double

Pierre GM pgmdevlist at gmail.com
Wed Oct 31 13:06:37 EDT 2007


On Wednesday 31 October 2007 09:50:27 Christopher Clarke wrote:
> Hi
> What's the most efficient way of casting an object array to a double
> ndarray?
> other than
> numpy.array(obj_arr.tolist(),dtype='d')


Something like that ?
>>>import numpy
>>>obj_list = [(1,1),(2,2),(None,3)]
>>>obj_arr=numpy.array(obj_list, dtype=numpy.object_)
>>>obj_arr
array([[1, 1], 
       [2, 2],
       [None, 3]], dtype=object)


With numpy.core.ma
>>>import numpy.core.ma as ma
>>>obj_flt = obj_arr.astype(numpy.float_)
>>>obj_mask = ma.array(obj_flt, mask=numpy.isnan(obj_flt))
>>>obj_mask
array(data =
 [[  1.00000000e+00   1.00000000e+00]
 [  2.00000000e+00   2.00000000e+00]
 [  1.00000000e+20   3.00000000e+00]],
      mask =
 [[False False]
 [False False]
 [ True False]],
      fill_value=1e+20)


With maskedarray (the now infamous alternative way, still in scipy SVN)
>>>import maskedarray
>>>obj_mask = maskedarray.fix_invalid(obj_arr.astype(numpy.float_))
>>>obj_mask
masked_array(data =
 [[1.0 1.0]
 [2.0 2.0]
 [-- 3.0]],
      mask =
 [[False False]
 [False False]
 [ True False]],
      fill_value=1e+20)     

maskedarray.fix_invalid will transform nans and infs in your array into floats 
and mask the corresponding values. The numpy.core.ma will keep the nans and 
infs in the data and will only mask them.

> The  original data is a list of tuples form a SQL query which could
> potentially contain None

The trick of going through float w/ astype should work OK if your tuples don't 
have strings hidden somewhere.



More information about the SciPy-User mailing list