casting ndarray object to double
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') The original data is a list of tuples form a SQL query which could potentially contain None Indeed a masked array would be more ideal where None is replaced by the mask I'm currently using list comprehension to replace the None in the original data with the mask But i wish to eliminate this step if possible Regards Chris
Hi, Did you try numpy.asfarray(obj_arr) ? Matthieu 2007/10/31, Christopher Clarke <cclarke@chrisdev.com>:
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')
The original data is a list of tuples form a SQL query which could potentially contain None Indeed a masked array would be more ideal where None is replaced by the mask I'm currently using list comprehension to replace the None in the original data with the mask But i wish to eliminate this step if possible Regards Chris
_______________________________________________ SciPy-user mailing list SciPy-user@scipy.org http://projects.scipy.org/mailman/listinfo/scipy-user
-- French PhD student Website : http://miles.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
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.
participants (3)
-
Christopher Clarke
-
Matthieu Brucher
-
Pierre GM