numpy.promote_types#
- numpy.promote_types(type1,type2)#
Returns the data type with the smallest size and smallest scalarkind to which both
type1andtype2may be safely cast.The returned data type is always considered “canonical”, this mainlymeans that the promoted dtype will always be in native byte order.This function is symmetric, but rarely associative.
- Parameters:
- type1dtype or dtype specifier
First data type.
- type2dtype or dtype specifier
Second data type.
- Returns:
- outdtype
The promoted data type.
See also
Notes
Please see
numpy.result_typefor additional information about promotion.Starting in NumPy 1.9, promote_types function now returns a valid stringlength when given an integer or float dtype as one argument and a stringdtype as another argument. Previously it always returned the input stringdtype, even if it wasn’t long enough to store the max integer/float valueconverted to a string.
Changed in version 1.23.0.
NumPy now supports promotion for more structured dtypes. It will nowremove unnecessary padding from a structure dtype and promote includedfields individually.
Examples
>>>importnumpyasnp>>>np.promote_types('f4','f8')dtype('float64')
>>>np.promote_types('i8','f4')dtype('float64')
>>>np.promote_types('>i8','<c8')dtype('complex128')
>>>np.promote_types('i4','S8')dtype('S11')
An example of a non-associative case:
>>>p=np.promote_types>>>p('S',p('i1','u1'))dtype('S6')>>>p(p('S','i1'),'u1')dtype('S4')