[Numpy-discussion] how to find out element size of flexible dtype

Francesc Alted faltet@pytables....
Mon Oct 18 02:37:53 CDT 2010


A Monday 18 October 2010 03:42:39 Koert Kuipers escrigué:
> Hello all,
> If my function receives an array with a flexible dtype such as string
> or unicode, how do I find out what the size (max number of elements,
> not number of bytes) of the datatype is?
> 
> For example:
> >>> x = np.array(['a', 'b', 'cd'], (np.unicode_, 4))
> >>> x.dtype
> 
> dtype('<U4')
> 
> The string representation of x.dtype shows that is has element size
> 4, so I could get the element size by parsing that, but is there a
> more elegant way to get this? Thanks,

Not very elegant, but it works:

>>> x.itemsize / np.dtype((x.dtype.type, 1)).itemsize
4

Cheers,

-- 
Francesc Alted


More information about the NumPy-Discussion mailing list