Skip to content

can_cast

Determines if one data type can be cast to another data type

Parameters:

Name Type Description Default
from_ dtype or SparseArray

Source array or dtype.

required
to dtype

Destination dtype.

required
casting str

Casting kind

'safe'

Returns:

Name Type Description
out bool_

Whether or not a cast is possible.

Examples:

>>> x = sparse.ones((2, 3), dtype=sparse.int8)
>>> sparse.can_cast(x, sparse.float64)
True
See Also
Source code in sparse/numba_backend/_common.py
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
def can_cast(from_: SparseArray, to: np.dtype, /, *, casting: str = "safe") -> bool:
    """Determines if one data type can be cast to another data type

    Parameters
    ----------
    from_ : dtype or SparseArray
        Source array or dtype.
    to : dtype
        Destination dtype.
    casting: str
        Casting kind

    Returns
    -------
    out : bool
        Whether or not a cast is possible.

    Examples
    --------
    >>> x = sparse.ones((2, 3), dtype=sparse.int8)
    >>> sparse.can_cast(x, sparse.float64)
    True

    See Also
    --------
    - [`numpy.can_cast`][] : NumPy equivalent function
    """
    from_ = np.dtype(from_)

    return np.can_cast(from_, to, casting=casting)