#[derive(ToDataFrame)]
{
// Attributes available to this derive:
#[df_derive]
}
Expand description
Derive ToDataFrame for structs and tuple structs to generate fast conversions to Polars.
What this macro generates (paths configurable via #[df_derive(...)]):
- An implementation of
ToDataFramefor the annotated typeTproviding:fn to_dataframe(&self) -> PolarsResult<DataFrame>fn empty_dataframe() -> PolarsResult<DataFrame>fn schema() -> PolarsResult<Vec<(&'static str, DataType)>>
- An implementation of
ColumnarforTprovidingfn columnar_to_dataframe(items: &[Self]) -> PolarsResult<DataFrame>
Supported shapes and types:
- Named and tuple structs (tuple fields are named
field_{index}) - Nested structs are flattened using dot notation (e.g.,
outer.inner) - Wrappers
Option<T>andVec<T>in any nesting order, withVec<Struct>producing multiple list columns with avec_field.subfieldprefix - Primitive types:
String,bool, integer types,f32,f64 chrono::DateTime<Utc>(materialized asDatetime(Milliseconds, None))rust_decimal::Decimal(materialized asDecimal(38, 10))
Attributes:
- Container-level:
#[df_derive(trait = "path::ToDataFrame")]to set theToDataFrametrait path; theColumnarpath is inferred by replacing the last path segment withColumnar. Optionally, set both explicitly with#[df_derive(columnar = "path::Columnar")]. - Field-level:
#[df_derive(as_string)]to stringify values (e.g., enums) during conversion, resulting inDataType::StringorList<String>.
Notes:
- Enums and generic structs are not supported for derive.
- All nested custom structs must also derive
ToDataFrame. - Empty structs:
to_dataframeyields a single-row, zero-columnDataFrame; the columnar path yields a zero-columnDataFramewithitems.len()rows.