ToDataFrame

Derive Macro ToDataFrame 

Source
#[derive(ToDataFrame)]
Expand description

Derive macro for automatic DataFrame conversion.

Generates a to_dataframe(&self) -> PolarsResult<DataFrame> method that converts all struct fields to DataFrame columns.

§Supported Types

  • String → String column
  • Option<String> → nullable String column
  • Option<FormattedValue<f64>> → extracts .raw as Option<f64>
  • Option<FormattedValue<i64>> → extracts .raw as Option<i64>
  • i32, i64, f64, bool → direct columns
  • Option<T> for primitives → nullable columns
  • Nested structs/Vec → skipped (complex types not suitable for flat DataFrame)

§Example

#[derive(ToDataFrame)]
pub struct Quote {
    pub symbol: String,
    pub price: Option<FormattedValue<f64>>,
}

// Generates:
impl Quote {
    pub fn to_dataframe(&self) -> PolarsResult<DataFrame> {
        df![
            "symbol" => [self.symbol.as_str()],
            "price" => [self.price.as_ref().and_then(|v| v.raw)],
        ]
    }
}