- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- Return True if cast between scalar and data type can occur according to the casting rule in Python
- Return True if cast between array scalar and data type can occur according to the casting rule in Python
- Return the scalar type of highest precision of the same kind as the input in Python
- Return True if cast between data types can occur controlling what kind of data casting may occur in Python
- Return a scalar type which is common to the input arrays in Python
- Return True if cast between data types can occur according to the casting rule in Python
- Determine whether the given object represents a scalar data-type in Python
- Return a description for the given data type code in Python
- Find the minimal data type of a scalar value in Python
- Return the scalar dtype or NumPy equivalent of Python type of an object
- Return the copy of a masked array cast to a specified type in Numpy
- Return a new array of given shape filled with ones and also set the desired data-type in Numpy
- Find the lexicographically smallest string which satisfies the given condition in Python
- Return the string representation of a scalar dtype in Python
- Write the smallest number which is divisible by both 306 and 657.

Advertisements