Return the data type with the smallest size and scalar kind to which both the given types be safely cast in Python


The numpy.promote_types() method returns the data type with the smallest size and smallest scalar kind to which both type1 and type2 may be safely cast. Returns the promoted data type. The returned data type is always in native byte order. The 1st parameter is the first data type. The 2nd parameter is the second data type.

Steps

At first, import the required library −

import numpy as np

Checking with promote_types() method in Numpy −

print("Result...",np.promote_types('f4', 'f8'))
print("Result...",np.promote_types('i8', 'f4'))
print("Result...",np.promote_types('>i8', '<c8'))
print("Result...",np.promote_types('i4', 'S8'))
print("Result...",np.promote_types(np.int32, np.int64))
print("Result...",np.promote_types(np.float64, complex))
print("Result...",np.promote_types(complex, float))

Example

import numpy as np

# The numpy.promote_types() method returns the data type with the smallest size and smallest scalar kind to which both type1 and type2 may be safely cast.

print("Checking with promote_types() method in Numpy\n")
print("Result...",np.promote_types('f4', 'f8'))
print("Result...",np.promote_types('i8', 'f4'))
print("Result...",np.promote_types('>i8', '<c8'))
print("Result...",np.promote_types('i4', 'S8'))
print("Result...",np.promote_types(np.int32, np.int64))
print("Result...",np.promote_types(np.float64, complex))
print("Result...",np.promote_types(complex, float))

Output

Checking with promote_types() method in Numpy

Result... float64
Result... float64
Result... complex128
Result... |S11
Result... int64
Result... complex128
Result... complex128

Updated on: 25-Feb-2022

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements