asarray

asarray(obj: array | bool | int | float | complex | NestedSequence | SupportsBufferProtocol, /, *, dtype: dtype | None = None, device: device | None = None, copy: bool | None = None) array

Convert the input to an array.

Parameters:
  • obj (Union[array, bool, int, float, complex, NestedSequence[bool | int | float | complex], SupportsBufferProtocol]) –

    object to be converted to an array. May be a Python scalar, a (possibly nested) sequence of Python scalars, or an object supporting the Python buffer protocol.

    Tip

    An object supporting the buffer protocol can be turned into a memoryview through memoryview(obj).

  • dtype (Optional[dtype]) –

    output array data type. If dtype is None, the output array data type must be inferred from the data type(s) in obj. If all input values are Python scalars, then, in order of precedence,

    • if all values are of type bool, the output data type must be bool.

    • if all values are of type int or are a mixture of bool and int, the output data type must be the default integer data type.

    • if one or more values are complex numbers, the output data type must be the default complex floating-point data type.

    • if one or more values are floats, the output data type must be the default real-valued floating-point data type.

    Default: None.

    Note

    If dtype is not None, then array conversions should obey Type Promotion Rules rules. Conversions not specified according to Type Promotion Rules rules may or may not be permitted by a conforming array library. To perform an explicit cast, use array_api.astype().

    Note

    If an input value exceeds the precision of the resolved output array data type, behavior is left unspecified and, thus, implementation-defined.

  • device (Optional[device]) – device on which to place the created array. If device is None and obj is an array, the output array device must be inferred from obj. Default: None.

  • copy (Optional[bool]) – boolean indicating whether or not to copy the input. If True, the function must always copy. If False, the function must never copy for input which supports the buffer protocol and must raise a ValueError in case a copy would be necessary. If None, the function must reuse existing memory buffer if possible and copy otherwise. Default: None.

Returns:

out (array) – an array containing the data from obj.

Notes

Changed in version 2022.12: Added complex data type support.