- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 True if cast between data types can occur according to the casting rule in Python
The numpy.can_cast() method returns True if cast between data types can occur according to the casting rule. The 1st parameter is the data type or array to cast from. The 2nd parameter is the data type to cast to.
Steps
At first, import the required library −
import numpy as np
Using the can_cast() to check if cast between data types can occur according to the casting rule −
print("Checking with can_cast() method in Numpy\n") print("Result...",np.can_cast(np.int32, np.int64)) print("Result...",np.can_cast(np.float64, complex)) print("Result...",np.can_cast(complex, float)) print("Result...",np.can_cast('i8', 'f8')) print("Result...",np.can_cast('i8', 'f4')) print("Result...",np.can_cast('i4', 'S4'))
Example
import numpy as np # The numpy.can_cast() method returns True if cast between data types can occur according to the casting rule. # The 1st parameter is the data type or array to cast from. # The 2nd parameter is the data type to cast to. print("Checking with can_cast() method in Numpy\n") print("Result...",np.can_cast(np.int32, np.int64)) print("Result...",np.can_cast(np.float64, complex)) print("Result...",np.can_cast(complex, float)) print("Result...",np.can_cast('i8', 'f8')) print("Result...",np.can_cast('i8', 'f4')) print("Result...",np.can_cast('i4', 'S4'))
Output
Checking with can_cast() method in Numpy Result... True Result... True Result... False Result... True Result... False Result... False
Advertisements