w3resource

NumPy Data type: find_common_type() function

numpy.find_common_type() function

The find_common_type() function determine common type following standard coercion rules.

Version: 1.15.0

Syntax:

numpy.find_common_type(array_types, scalar_types)

Parameter:

Name Description Required /
Optional
array_types : sequence A list of dtypes or dtype convertible objects representing arrays. Required
scalar_types : sequence A list of dtypes or dtype convertible objects representing scalars. Required

Return value:

datatype : dtype
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.

Example: numpy.find_common_type() Function

>>> import numpy as np
>>> np.find_common_type([], [np.int64, np.float32, complex])
dtype('complex128')
>>> np.find_common_type([np.int64, np.float32], [])
dtype('float64')

The standard casting rules ensure that a scalar cannot up-cast an array unless the scalar is of a fundamentally different kind of data (i.e. under a different hierarchy in the data type hierarchy) then the array:

Example: numpy.find_common_type() function

>>> import numpy as np
>>> np.find_common_type([np.float32], [np.int64, np.float64])
dtype('float32')

Complex is of a different type, so it up-casts the float in the array_types argument:

Example: numpy.find_common_type() function

>>> import numpy as np
>>> np.find_common_type([np.float32], [complex])
dtype('complex128')

Type specifier strings are convertible to dtypes and can therefore be used instead of dtypes:

Example: numpy.find_common_type() function

>>> import numpy as np
>>> np.find_common_type(['f4', 'f4', 'i4'], ['c8'])
dtype('complex128')

Python - NumPy Code Editor:

Previous: issubclass_()
Next: Miscellaneous typename()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/numpy/data-type-routines/find_common_type.php