rust-ndarray / ndarray

ndarray: an N-dimensional array with array views, multidimensional slicing, and efficient operations
https://docs.rs/ndarray/
Apache License 2.0
3.43k stars 295 forks source link

Question: is there anyway to convert from python ndarray to rust ndarray? #1350

Closed WindSoilder closed 5 months ago

WindSoilder commented 5 months ago

Assume that I have something in python ndarray:

import pandas as pd
df =  pd.DataFrame({'a': [1, 2.0, 3.22]})
values = df.values

The value is something the following:

array([[1.  ],
       [2.  ],
       [3.22]])

I wonder is there any way to convert it into rust ndarray?

I'm thinking about covnert from python numpy ndarray to bytes(values.tobytes()) firstly, but I'm failed to convert from such bytes to ndarray.

adamreichold commented 5 months ago

If you want Rust code to interact directly with Python data structures, bindings for the Python interpreter are required. PyO3 is one such project for which an add-on crate numpy is available which supports direct access of NumPy arrays as ArrayViews.

WindSoilder commented 5 months ago

Oh, sorry I'm asking in a wrong place

adamreichold commented 5 months ago

I would not say that, i.e. asking questions about interop with ndarray here is fine. It is just that the answers are elsewhere.