[Numpy-discussion] Type Inference

Charles R Harris charlesr.harris@gmail....
Wed Nov 25 23:51:29 CST 2009

On Wed, Nov 25, 2009 at 12:34 PM, Dan Yamins <dyamins@gmail.com> wrote:

> Sometimes I need to convert object-type arrays to their "natural, real"
> type, without a priori knowing what that type is, e.g.  the equivalent of:
> >>> Y = np.array(X.tolist())
> where X is the object array.  If X is naturally an array of ints, Y will be
> an int array, if X is naturally strings, then Y will be '|Sn' where n is the
> right string length, the right casting will be done if there are mixed
> types, etc...
> My question is: is there a faster way to do this, given that X is already
> an object array.
> One possible (though maybe not the only) way this might work is if there is
> a faster lower-level numpy type-inference function that doesn't actually do
> the conversion, but just reports the "right" type, the type that np.array()
> *would* convert to. Does such a thing exist?  And if so, (let's call it
> 'np.typeinferer', hypothetically), would it then be faster to do something
> like:
>    >>> Y = np.array(X,np.typeinferer(X))
> by-passing the step of converting the list-ified version of X to the new
> type?   Or is the bulk of the work being done by np.array() the type
> inference anyway, so that it just makes sense to use np.array() in the first
> place?
> Sorry if this question is unclear -- and thanks for all the help recently.
This seems to work:

In [5]: x = ones(2, dtype=object)

In [6]: x
Out[6]: array([1, 1], dtype=object)

In [7]: array(x, dtype=int)
Out[7]: array([1, 1])

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20091125/3e2a666e/attachment.html 

More information about the NumPy-Discussion mailing list