w3resource

NumPy Data type: promote_types() function

numpy.promote_types() function

The NumPy promote_types() function returns the data type with the smallest size and smallest scalar kind to which both type1 and type2 may be safely cast. The returned data type is always in native byte order.

This function is symmetric, but rarely associative.

Version: 1.15.0

Syntax:

numpy.promote_types(type1, type2)

Parameter:

Name Description Required /
Optional
type1 First data type. Required
type2 Second data type. Required

Return value:

out : dtype - The promoted data type.

Example-1: numpy.promote_types() function

>>> import numpy as np
>>> 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')

Pictorial Presentation:

NumPy Data type: promote_types()

Pictorial Presentation:

NumPy Data type: promote_types()

Example-2: numpy.promote_types() function

>>> import numpy as np
>>> p = np.promote_types
>>> p('S', p('i1', 'u1'))
dtype('S6')
>>> p(p('S', 'i1'), 'u1')
dtype('S4')

Pictorial Presentation:

NumPy Data type: promote_types()

Pictorial Presentation:

NumPy Data type: promote_types()

Python - NumPy Code Editor:

Previous: Data type can_cast()
Next: min_scalar_type()



Follow us on Facebook and Twitter for latest update.