pyo3_polars/lib.rs
1//! This crate offers a [`PySeries`] and a [`PyDataFrame`] which are simple wrapper around `Series` and `DataFrame`. The
2//! advantage of these wrappers is that they can be converted to and from python as they implement `FromPyObject` and `IntoPy`.
3//!
4//! # Example
5//!
6//! From `src/lib.rs`.
7//! ```rust
8//! # use polars::prelude::*;
9//! # use pyo3::prelude::*;
10//! # use pyo3_polars::PyDataFrame;
11//!
12//! #[pyfunction]
13//! fn my_cool_function(pydf: PyDataFrame) -> PyResult<PyDataFrame> {
14//! let df: DataFrame = pydf.into();
15//! let df = {
16//! // some work on the dataframe here
17//! todo!()
18//! };
19//!
20//! // wrap the dataframe and it will be automatically converted to a python polars dataframe
21//! Ok(PyDataFrame(df))
22//! }
23//!
24//! /// A Python module implemented in Rust.
25//! #[pymodule]
26//! fn expression_lib(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
27//! m.add_function(wrap_pyfunction!(my_cool_function, m)?)?;
28//! Ok(())
29//! }
30//! ```
31//!
32//! Compile your crate with `maturin` and then import from python.
33//!
34//! From `my_python_file.py`.
35//! ```python
36//! from expression_lib import my_cool_function
37//!
38//! df = pl.DataFrame({
39//! "foo": [1, 2, None],
40//! "bar": ["a", None, "c"],
41//! })
42//! out_df = my_cool_function(df)
43//! ```
44mod alloc;
45#[cfg(feature = "derive")]
46pub mod derive;
47pub mod error;
48#[cfg(feature = "derive")]
49pub mod export;
50mod ffi;
51mod types;
52
53pub use crate::alloc::PolarsAllocator;
54use once_cell::sync::Lazy;
55use pyo3::prelude::*;
56pub use types::*;
57
58pub(crate) static POLARS: Lazy<Py<PyModule>> =
59 Lazy::new(|| Python::with_gil(|py| PyModule::import(py, "polars").unwrap().unbind()));
60
61pub(crate) static SERIES: Lazy<Py<PyAny>> =
62 Lazy::new(|| Python::with_gil(|py| POLARS.getattr(py, "Series").unwrap()));