# Determine common type following standard coercion rules in Python

To determine common type following standard coercion rules, use the numpy.find_common_type() method in Python numpy. The 1st argument is a list of dtypes or dtype convertible objects representing arrays. The 2nd argument is A list of dtypes or dtype convertible objects representing scalars.

The find_common_type() method returns the common data type, which is the maximum of array_types ignoring scalar_types, unless the maximum of scalar_types is of a different kind (dtype.kind). If the kind is not understood, then None is returned.

## Steps

At first, import the required library −

import numpy as np

Using the find_common_type() method in Numpy. Determine common type following standard coercion rules −

print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([], [np.int64, np.float32, complex]))
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([np.float32], [complex]))
print("Result...",np.find_common_type([np.float64], [complex]))
print("Result...",np.find_common_type(['f4', 'i4'], ['c8']))
print("Result...",np.find_common_type([np.int64], [complex]))
print("Result...",np.find_common_type([np.int64], [np.float64]))

## Example

import numpy as np

# To determine common type following standard coercion rules, use the numpy.find_common_type() method in Python numpy
# The 1st argument is a list of dtypes or dtype convertible objects representing arrays.
# The 2nd argument is A list of dtypes or dtype convertible objects representing scalars.

print("Using the find_common_type() method in Numpy\n")

# Determine common type following standard coercion rules
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([], [np.int64, np.float32, complex]))
print("Result...",np.find_common_type([np.float32], [np.int64, np.float64]))
print("Result...",np.find_common_type([np.float32], [complex]))
print("Result...",np.find_common_type([np.float64], [complex]))
print("Result...",np.find_common_type(['f4', 'i4'], ['c8']))
print("Result...",np.find_common_type([np.int64], [complex]))
print("Result...",np.find_common_type([np.int64], [np.float64]))

## Output

Using the find_common_type() method in Numpy

Result... float32
Result... complex128
Result... float32
Result... complex128
Result... complex128
Result... complex128
Result... complex128
Result... float64

Advertisements