Home > database >  How can I get the coerced dtype for a list of arrays much like np.asarray()?
How can I get the coerced dtype for a list of arrays much like np.asarray()?

Time:04-20

I have a list of ndarrays which may have different dtypes. If they have the same size, I can call np.asarray(list_of_ndarrays) to get a "stacked" ndarray with an appropriate ("larger", coerced) dtype, for instance:

[uint16, uint8, uint16, uint8, uint16, uint16, uint16, uint16, int16, uint16] -> int32

Now, for lists of arrays with varying sizes, I have written a unify_sizes() method which mangles the arrays into a common size (with different modes such as minimum / maximum size), and this method needs to allocate a resulting buffer with the right dtype.

(How) Can I perform the same type coercion that asarray() does?

I traced the code of asarray() down into PyArray_DiscoverDTypeAndShape(), and I wondered if there was a python frontend for that method?

(I know that I could always first unify sizes into a list, then call np.asarray() on that list, but the arrays can be largish, and I might need to do padding, so I would like to prevent the extra copying if possible.)

CodePudding user response:

A couple of provided functions:

In [30]: np.promote_types(*[np.uint8, np.uint16])
Out[30]: dtype('uint16')

In [31]: alist = [np.ones(2,'uint8'), np.ones(3, 'uint16')]
In [32]: np.result_type(*alist)
Out[32]: dtype('uint16')

CodePudding user response:

One way I could think of was to actually call np.asarray() on a list of arrays of shape 1 with the same dtypes as my actual input, but that does not seem to be very elegant.

  • Related