跳到内容

can_cast

确定一种数据类型是否可以转换为另一种数据类型

参数

名称 类型 描述 默认值
from_ dtypeSparseArray

源数组或数据类型。

必需
to 数据类型

目标数据类型。

必需
casting 字符串

转换类型

'安全'

返回值

名称 类型 描述
out 布尔值

是否可以进行转换。

示例

>>> x = sparse.ones((2, 3), dtype=sparse.int8)
>>> sparse.can_cast(x, sparse.float64)
True
另请参阅
源代码位于 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)