Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extract arrays from a Python object? #242

Closed
zmitchell opened this issue Jan 6, 2022 · 1 comment · Fixed by #265
Closed

How to extract arrays from a Python object? #242

zmitchell opened this issue Jan 6, 2022 · 1 comment · Fixed by #265

Comments

@zmitchell
Copy link

If I have a Python dictionary (i.e. a PyDict), and I call get_item on it I get back a &PyAny. How do I go about converting this to a PyArray?

All of the documentation contains examples where NumPy arrays are passed directly to the Rust extension, but I haven't seen any where they are attributes of a class or stored as a reference, in other words when there is a layer of indirection between the NumPy array and the Rust extension. I would be glad to submit an example once I know how to do this.

@adamreichold
Copy link
Member

adamreichold commented Jan 6, 2022

I would expect that you would be able to downcast the &PyAny returned by get_item into the array type, e.g.

fn array_in_dict(dict: &PyDict) {
    let array = dict
        .get_item(42)
        .unwrap()
        .downcast::<PyArray2<f64>>()
        .unwrap();

    ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants