Trait numpy::convert::ToPyArray[][src]

pub trait ToPyArray {
    type Item: Element;
    type Dim: Dimension;
    fn to_pyarray<'py>(
        &self,
        _: Python<'py>
    ) -> &'py PyArray<Self::Item, Self::Dim>; }
Expand description

Conversion trait from rust types to PyArray.

This trait takes &self, which means it alocates in Python heap and then copies elements there.

Example

use numpy::{PyArray, ToPyArray};
pyo3::Python::with_gil(|py| {
    let py_array = vec![1, 2, 3].to_pyarray(py);
    assert_eq!(py_array.readonly().as_slice().unwrap(), &[1, 2, 3]);
});

This method converts a not-contiguous array to C-order contiguous array.

Example

use numpy::{PyArray, ToPyArray};
use ndarray::{arr3, s};
pyo3::Python::with_gil(|py| {
    let a = arr3(&[[[ 1,  2,  3], [ 4,  5,  6]],
                   [[ 7,  8,  9], [10, 11, 12]]]);
    let slice = a.slice(s![.., 0..1, ..]);
    let sliced = arr3(&[[[ 1,  2,  3]],
                        [[ 7,  8,  9]]]);
    let py_slice = slice.to_pyarray(py);
    assert_eq!(py_slice.readonly().as_array(), sliced);
    pyo3::py_run!(py, py_slice, "assert py_slice.flags['C_CONTIGUOUS']");
});

Associated Types

Required methods

Implementations on Foreign Types

Implementors