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

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

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

Loading content...

Required methods

pub fn to_pyarray<'py>(
    &self,
    _: Python<'py>
) -> &'py PyArray<Self::Item, Self::Dim>
[src]

Loading content...

Implementations on Foreign Types

impl<T: Element> ToPyArray for [T][src]

type Item = T

type Dim = Ix1

impl<S, D, A> ToPyArray for ArrayBase<S, D> where
    S: Data<Elem = A>,
    D: Dimension,
    A: Element
[src]

type Item = A

type Dim = D

Loading content...

Implementors

Loading content...