[Numpy-discussion] Type Inference

Dan Yamins dyamins at gmail.com
Wed Nov 25 14:34:36 EST 2009

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

   >>> 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

Sorry if this question is unclear -- and thanks for all the help recently.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20091125/9db53dd9/attachment.html>

More information about the NumPy-Discussion mailing list