[Numpy-discussion] find appropriate dtype based on a set of values

Gregorio Bastardo gregorio.bastardo at gmail.com
Mon Sep 2 10:21:11 EDT 2013


Hi,

I'd like to find the smallest possible representation of an array
given a set of possible values. I've checked the function
'np.min_scalar_type', it works well for scalar input, but contrary to
my assumption when array-like param is given: array's dtype is simply
returned, instead of finding a more compact dtype for the array
values.

>>> np.version.version
'1.7.0'

>>> np.min_scalar_type(0) # ok
dtype('uint8')

>>> np.min_scalar_type(-1) # ok
dtype('int8')

>>> np.min_scalar_type( [0,-1] ) # int8 expected, returns platform-default int
dtype('int32')

>>> np.min_scalar_type( [0,256] ) # uint16 expected
dtype('int32')

>>> np.min_scalar_type([-1,256]) # int16 expected
dtype('int32')

Am I missing something? Anyone knows how to achieve the desired operation?

Thanks a lot,
Gregorio



More information about the NumPy-Discussion mailing list