Crate pyo3_polars

source ·
Expand description

This crate offers a PySeries and a PyDataFrame which are simple wrapper around Series and DataFrame. The advantage of these wrappers is that they can be converted to and from python as they implement FromPyObject and IntoPy.

§Example

From src/lib.rs.


#[pyfunction]
fn my_cool_function(pydf: PyDataFrame) -> PyResult<PyDataFrame> {
    let df: DataFrame = pydf.into();
    let df = {
        // some work on the dataframe here
        todo!()
    };

    // wrap the dataframe and it will be automatically converted to a python polars dataframe
    Ok(PyDataFrame(df))
}

/// A Python module implemented in Rust.
#[pymodule]
fn expression_lib(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(my_cool_function, m)?)?;
    Ok(())
}

Compile your crate with maturin and then import from python.

From my_python_file.py.

from expression_lib import my_cool_function

df = pl.DataFrame({
    "foo": [1, 2, None],
    "bar": ["a", None, "c"],
})
out_df = my_cool_function(df)

Modules§

Structs§

  • A wrapper around a DataFrame that can be converted to and from python with pyo3.
  • A wrapper around a Series that can be converted to and from python with pyo3.