ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> numpy-discussion
numpy-discussion
Re: [Numpy-discussion] array dtype=object problem
by Travis Oliphant other posts by this author
Jan 28 2006 3:05PM messages near this date
[Numpy-discussion] array dtype=object problem | [Numpy-discussion] Linux Installation Instructions
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



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@[...].net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion
Thread:
Christopher Fonnesbeck
Travis Oliphant

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved