Python | Numpy np.can_cast() method (original) (raw)
Last Updated : 03 Nov, 2019
With the help of **np.can_cast()**
method, we can get the perfect idea that one data type can be able to cast into another data type or not by using np.can_cast()
method.
Syntax :
np.can_cast(source data_type, target data_type)
Return : Return the boolean value as true when casting can be done else false.
Example #1 :
In this example we can see that by using np.can_cast()
method, we are able to get the boolean value as true when casting can be performed else false by using this method.
import
numpy as np
gfg
=
np.can_cast(np.int32, np.int64)
print
(gfg)
Output :
True
Example #2 :
import
numpy as np
gfg
=
np.can_cast(
5.5e10
, np.int32)
print
(gfg)
Output :
False
Similar Reads
- Python | Numpy np.legzero() method np.legzero() method can be used instead of np.zeros for creating a array whose elements are 0. Syntax : np.legzero() Return : Return array([0]) Example #1 : # Python program explaining # numpy.legzero() method # import numpy and legzero import numpy as np from numpy.polynomial.legendre import legzer 1 min read
- Python - Numpy fromrecords() method numpy.fromrecords() method is a powerful tool in the NumPy library that allows you to create structured arrays from a sequence of tuples or other array-like objects. Let's understand the help of an example: [GFGTABS] Python import numpy as np # Define a list of records records = [(1, 'Alice' 2 min read
- Python | Decimal is_nan() method Decimal#is_nan() : is_nan() is a Decimal class method which checks whether the Decimal value is NaN value. Syntax: Decimal.is_nan() Parameter: Decimal values Return: true - if the Decimal value is NaN value; otherwise false Code #1 : Example for is_nan() method # Python Program explaining # is_nan() 2 min read
- numpy.atleast_1d() in Python numpy.atleast_1d()function is used when we want to Convert inputs to arrays with at least one dimension. Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved. Syntax : numpy.atleast_1d(*arrays) Parameters : arrays1, arrays2, ... : [array_like] One or mo 2 min read
- numpy.atleast_2d() in Python numpy.atleast_2d() function is used when we want to Convert inputs to arrays with at least two dimension. Scalar and 1-dimensional inputs are converted to 2-dimensional arrays, whilst higher-dimensional inputs are preserved. Syntax : numpy.atleast_2d(*arrays) Parameters : arrays1, arrays2, ... : [ar 2 min read
- Numpy MaskedArray asarray() method | Python numpy.ma.asarray() function is used when we want to convert input to a masked array of the given data-type. No copy is performed if the input is already a ndarray. If arr is a subclass of MaskedArray, a base class MaskedArray is returned. Syntax : numpy.ma.asarray(arr, dtype=None, order=None) Parame 2 min read
- numpy.any() in Python The numpy.any() function tests whether any array elements along the mentioned axis evaluate to True. Syntax : numpy.any(a, axis = None, out = None, keepdims = class numpy._globals._NoValue at 0x40ba726c) Parameters : array :[array_like]Input array or object whose elements, we need to test. axis : [i 3 min read
- Numpy MaskedArray asanyarray() method | Python numpy.ma.asanyarray() function is used when we want to convert input to a masked array, conserving subclasses. If arr is a subclass of MaskedArray, its class is conserved. No copy is performed if the input is already an ndarray. Syntax : numpy.ma.asanyarray(arr, dtype=None) Parameters : arr : [array 2 min read
- numpy.around() in Python numpy.around(arr, decimals = 0, out = None) : This mathematical function helps user to evenly round array elements to the given number of decimals. Parameters : array : [array_like] Input array. decimal : [int, optional] Decimal places we want to round off. Default = 0. In case of -ve decimal, it sp 2 min read
- numpy.broadcast_to() function – Python numpy.broadcast_to() function broadcast an array to a new shape. Syntax : numpy.broadcast_to(array, shape, subok = False) Parameters : array : [array_liket] The array to broadcast. shape : [tuple] The shape of the desired array. subok : [bool, optional] If True, then sub-classes will be passed-throu 1 min read