array dtype=object problem
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: (Pdb) y[:5] array([575.060695363, 618.395159954, 817.767177867, 601.659726372, 978.607427424], dtype=object) (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. Thanks, C. -- Christopher J. Fonnesbeck Population Ecologist, Marine Mammal Section Fish & Wildlife Research Institute (FWC) St. Petersburg, FL Adjunct Assistant Professor Warnell School of Forest Resources University of Georgia Athens, GA T: 727.235.5570 E: chris at trichech.us
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 error. The .astype(to_dtype) method of numpy arrays will always convert (force-cast) to the request type. So, y.astype(float) will work as you want. -Travis
participants (2)
-
Christopher Fonnesbeck
-
Travis Oliphant