Skip to main content

from_polars

Macro from_polars 

Source
macro_rules! from_polars {
    ($df:expr) => { ... };
}
Expand description

Convert a polars DataFrame into plot-ready columns using your crate’s polars (#466): the macro expands in your crate, against the polars version you depend on, so it works whether you use polars 0.46, 0.52, 0.53 or later — no second polars build, no type mismatch, and ggplot-rs’s own polars feature can stay off.

Returns PolarsResult<Vec<(String, Vec<Value>)>>, which is GGData, so it goes straight into GGPlot::new:

ⓘ
use ggplot_rs::prelude::*;
let df = polars::df!("x" => [1, 2, 3], "y" => [2.0, 1.0, 3.0])?;
let plot = GGPlot::new(ggplot_rs::from_polars!(&df)?)
    .aes(Aes::new().x("x").y("y"))
    .geom_point();

Booleans, strings, integers, floats, Date (→ seconds) and Datetime (any unit → seconds) convert natively; every other dtype (categoricals, decimals, nested …) is cast to its string form. Needs polars in scope as ::polars with its (default) dtype-date / dtype-datetime features.