Skip to content

ndarray equivalent of numpy.astype? #493

@drewm1980

Description

@drewm1980

How does one convert the type of an array? We're trying to go from an owned Array2<u32> to an owned Array2<usize>.

We've tried to_owned(), into_owned(), and from(), and can't find anything that works. Do we need to resort to iterators for this?

As a documentation feature request, could you please add the answer to the "ndarray_for_numpy_users" page?

The wider context is that we're trying to get our data out of python using the rust-numpy crate:

https://github.com/rust-numpy/rust-numpy

but their example doesn't get the data all the way into an owned ndarray; it's still a view into a custom type wrapping data that is still owned by a python interpreter.

It already took us hours to get this far:

    let a = pyarray.as_array().unwrap();
    let b:ArrayView2<i32> = a.into_dimensionality::<Ix2>().unwrap();
    let c:ndarray::Array2<i32> = b.to_owned();

I'm hoping you can get us the rest of the way there, or maybe even point us to a conversion path that only incurs one copy rather than two...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions