abracudabra.conversion.carray

abracudabra.conversion.carray#

Convert to numpy or cupy arrays.

Functions#

to_array(sequence, /[, device, strict])

Convert an array, series, or dataframe to a NumPy or CuPy array.

Module Contents#

abracudabra.conversion.carray.to_array(sequence, /, device=None, *, strict=False)[source]#

Convert an array, series, or dataframe to a NumPy or CuPy array.

Parameters:
  • sequence (abracudabra._annotations.Array | abracudabra._annotations.Series | abracudabra._annotations.DataFrame | torch.Tensor) – The sequence to convert.

  • device (str | abracudabra.device.base.Device | None) – The device to convert the sequence to. If None, the sequence stays on the same device.

  • strict (bool) – Whether to raise an error if the sequence is not a valid type. A NumPy/CuPy array, Pandas/cuDF series or dataframe, or Torch tensor are valid types. If False, the sequence is converted to a NumPy/CuPy array if possible, but it might raise an error if the conversion is not possible.

Returns:

A NumPy/CuPy array.

Raises:

TypeError – If the sequence is not a valid type and strict is True.

Return type:

abracudabra._annotations.Array

Examples

Build a CuPy array from a sequence

>>> import cupy as cp
>>> cupy_array = to_array([1, 2, 3], "cuda:0")
>>> print(type(cupy_array))
<class 'cupy.ndarray'>

Build a NumPy array from a cuDF series

>>> import cudf
>>> cudf_series = cudf.Series([1, 2, 3])
>>> numpy_array = to_array(cudf_series)
>>> print(type(numpy_array))
<class 'numpy.ndarray'>