2

I have a Python function such as

def add_data(input_df): """ some manipulation of input_df (Polars dataframe) such as filling some columns with new values """

I would like to use this function from a Rust function. input_df can be tens of megabytes big, so I'd like to use zero-copy share between Python and Rust. Is there any example code on this available?

I found Is it possible to access underlying data from Polars in cython? but this seems to be Cython. I am looking for a pure Python way.

Rick Kim
  • 21
  • 2
  • Did you look at [ffi.rs](https://github.com/pola-rs/polars/blob/master/examples/python_rust_compiled_function/src/ffi.rs#L34) and lib.rs? It uses [PyO3](https://pyo3.rs/v0.18.0/python_from_rust.html#calling-python-functions) – jqurious Jan 31 '23 at 03:20

1 Answers1

1

I made a crate for this to make this easy: https://github.com/pola-rs/pyo3-polars

See the examples to get started: https://github.com/pola-rs/pyo3-polars/tree/main/example

ritchie46
  • 10,405
  • 1
  • 24
  • 43
  • Thanks. I see that this example sends dataframe from Python to Rust. My use case is that Rust is the main program and it will use pyo3 to call a method of a Python class. Thus, DataFrame is sent from Rust to Python. Is any such example available as well? – Rick Kim Jan 31 '23 at 08:04
  • There is no example, but the same should be possible with those extension types. – ritchie46 Jan 31 '23 at 08:30