[Numpy-discussion] array dtype=object problem

Travis Oliphant oliphant.travis at ieee.org
Sat Jan 28 15:05:04 CST 2006

Christopher Fonnesbeck wrote:

> I am having trouble with numpy arrays that have (for some reason) an  
> "object" dtype. All that happens in my code is that float values are  
> appended to a multidimensional array, this array is transposed, and  
> the transposed array is sent to matplotlib. Unfortunately, the array  
> ends up being of type "object", which is not recognized by matplotlib:

I'm not sure why that is, but I seem to recall from code that you've 
sent me before that you intentionally use object arrays from time to 
time.   Object arrays stay object arrays unless you request 
differently.   But, object arrays will usually run more slowly than 
fixed-precision arrays.

> (Pdb) scatter(x,y)
> *** TypeError: function not supported for these types, and can't  
> coerce safely to supported types
> Moreover, this array, which clearly contains nothing but floats  
> cannot be cast to a float array:
> (Pdb) array(y,float)
> *** TypeError: array cannot be safely cast to required type
> Can anyone shed some light on this? I am utterly confused.

When using the array constructor to cast to another type, there is a 
check that is equivalent to the numpy command can_cast(from_dtype, 
to_dtype) that is run to see if the conversion is "safe".  Of course 
casting from an object (which could have infinite precision) to a 
fixed-precision floating-point data-type is not safe and you get this 

The .astype(to_dtype) method of numpy arrays will always convert 
(force-cast) to the request type.



will work as you want.


More information about the Numpy-discussion mailing list