Skip to content

astype

Copies an array to a specified data type irrespective of type-promotion rules.

Parameters:

Name Type Description Default
x

array to cast.

required
dtype

desired data type.

required
copy

specifies whether to copy an array when the specified dtype matches the data type of the input array x. If True, a newly allocated array is always returned. If False and the specified dtype matches the data type of the input array, the input array is returned; otherwise, a newly allocated array is returned. Default: True.

True
Notes
  • When casting a boolean input array to a real-valued data type, a value of True is cast to a real-valued number equal to 1, and a value of False must cast to a real-valued number equal to 0.

  • When casting a boolean input array to a complex floating-point data type, a value of True is cast to a complex number equal to 1 + 0j, and a value of False is cast to a complex number equal to 0 + 0j.

  • When casting a real-valued input array to bool, a value of 0 is cast to False, and a non-zero value is cast to True.

  • When casting a complex floating-point array to bool, a value of 0 + 0j is cast to False, and all other values are cast to True.

Returns:

Name Type Description
out array

an array having the specified data type. The returned array has the same shape as x.

Examples:

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.astype(a, "float32")
>>> o.todense()
array([[0., 1.],
       [2., 0.]], dtype=float32)
Source code in sparse/numba_backend/_common.py
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
def astype(x, dtype, /, *, copy=True):
    """
    Copies an array to a specified data type irrespective of type-promotion rules.

    Parameters
    ----------
    x: array
        array to cast.
    dtype: dtype
        desired data type.
    copy: bool
        specifies whether to copy an array when the specified ``dtype`` matches the data type
        of the input array ``x``. If ``True``, a newly allocated array is always returned.
        If ``False`` and the specified ``dtype`` matches the data type of the input array,
        the input array is returned; otherwise, a newly allocated array is returned.
        Default: ``True``.

    Notes
    -----

       - When casting a boolean input array to a real-valued data type, a value of ``True`` is cast
         to a real-valued number equal to ``1``, and a value of ``False`` must cast to a real-valued
         number equal to ``0``.

       - When casting a boolean input array to a complex floating-point data type, a value of ``True``
       is cast to a complex number equal to ``1 + 0j``, and a value of ``False`` is cast to a complex
       number equal to ``0 + 0j``.

       - When casting a real-valued input array to ``bool``, a value of ``0`` is cast to ``False``,
       and a non-zero value is cast to ``True``.

       - When casting a complex floating-point array to ``bool``, a value of ``0 + 0j`` is cast
       to ``False``, and all other values are cast to ``True``.

    Returns
    -------
    out: array
        an array having the specified data type. The returned array has the same shape as ``x``.

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.astype(a, "float32")
    >>> o.todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[0., 1.],
           [2., 0.]], dtype=float32)
    """
    return x.astype(dtype, copy=copy)