anyvalue_dataframe/
lib.rs1#![doc(html_root_url = "https://docs.rs/anyvalue-dataframe/0.1.2")]
2use std::error::Error;
6use polars::prelude::{DataFrame, AnyValue, Schema, Field, DataType};
7
8#[macro_export]
10macro_rules! from_any {
11 ($a: expr, DataType::Int64) => {
12 match $a { AnyValue::Int64(i) => i, _ => 0 }
13 };
14 ($a: expr, DataType::Int32) => {
15 match $a { AnyValue::Int32(i) => i, _ => 0 }
16 };
17 ($a: expr, DataType::Int16) => {
18 match $a { AnyValue::Int16(i) => i, _ => 0 }
19 };
20 ($a: expr, DataType::Int8) => {
21 match $a { AnyValue::Int8(i) => i, _ => 0 }
22 };
23 ($a: expr, DataType::UInt64) => {
24 match $a { AnyValue::UInt64(u) => u, _ => 0 }
25 };
26 ($a: expr, DataType::UInt32) => {
27 match $a { AnyValue::UInt32(u) => u, _ => 0 }
28 };
29 ($a: expr, DataType::UInt16) => {
30 match $a { AnyValue::UInt16(u) => u, _ => 0 }
31 };
32 ($a: expr, DataType::UInt8) => {
33 match $a { AnyValue::UInt8(u) => u, _ => 0 }
34 };
35 ($a: expr, DataType::Float64) => {
36 match $a { AnyValue::Float64(f) => f, _ => 0.0 }
37 };
38 ($a: expr, DataType::Float32) => {
39 match $a { AnyValue::Float32(f) => f, _ => 0.0 }
40 };
41 ($a: expr, DataType::Utf8) => { match $a { AnyValue::Utf8(s) => s, _ => "" }
43 };
44 ($a: expr, DataType::String) => { match $a { AnyValue::String(s) => s, _ => "".to_string() }
46 };
47 ($a: expr, DataType::Boolean) => {
48 match $a { AnyValue::Boolean(b) => b, _ => false }
49 };
50 ($a: expr, DataType::BinaryOwned) => { match &$a {
52 AnyValue::BinaryOwned(u) => u.clone(),
53 AnyValue::Binary(u) => u.to_vec(),
54 _ => vec![]
55 }
56 };
57 ($a: expr, DataType::Binary) => { match &$a {
59 AnyValue::Binary(u) => u.to_vec(),
60 AnyValue::BinaryOwned(u) => u.clone(),
61 _ => vec![]
62 }
63 };
64 ($a: expr, DataType::Null) => { 0i64 }; ($a: expr, DataType::Unknown) => { 0i64 }; ($a: expr, DataType:: $t: ident) => { 0i64 } }
68#[macro_export]
74macro_rules! to_any {
75 ($v: expr, DataType::Null) => { AnyValue::Null };
76 ($v: expr, DataType:: $t: ident) => { AnyValue::$t($v) }
82}
83pub fn row_schema(row: Vec<AnyValue<'_>>) -> polars::frame::row::Row {
88 polars::frame::row::Row::new(row)
89}
90
91pub fn row_fields(desc: Vec<(&str, DataType)>) -> Vec<Field> {
94 desc.into_iter().map(|(s, t)| Field::new(s, t)).collect()
95}
96
97pub fn named_fields(df: &DataFrame, n: Vec<&str>) -> Vec<Field> {
99 let t = df.dtypes();
100 row_fields(n.into_iter().enumerate().map(|(i, s)|
101 (s, t[i].clone())).collect())
102}
103
104pub fn named_schema(df: &DataFrame, n: Vec<&str>) -> Schema {
108 Schema::from_iter(named_fields(&df, n))
109}
110
111pub fn df_from_vec(rows: &Vec<polars::frame::row::Row>, n: &Vec<&str>) ->
113 Result<DataFrame, Box<dyn Error>> {
114 let schema = Schema::from(&rows[0]);
115 let mut df = DataFrame::from_rows_iter_and_schema(rows.iter(), &schema)?;
116 df.set_column_names(&n)?;
117 Ok(df)
118}
119
120#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
127 fn test_dataframe() {
128 let a = to_any!(3, DataType::UInt64);
129 assert_eq!(a, AnyValue::UInt64(3));
130 assert_eq!(a.dtype(), DataType::UInt64);
131 let b = to_any!("A", DataType::Utf8);
132 assert_eq!(b, AnyValue::Utf8("A"));
133 assert_eq!(b.dtype(), DataType::Utf8);
134 let c = to_any!(4, DataType::Int8);
135 assert_eq!(c, AnyValue::Int8(4));
136 assert_eq!(c.dtype(), DataType::Int8);
137 let d = to_any!(1.5, DataType::Float64);
138 assert_eq!(d, AnyValue::Float64(1.5));
139 assert_eq!(d.dtype(), DataType::Float64);
140 let e = to_any!(true, DataType::Boolean);
141 assert_eq!(e, AnyValue::Boolean(true));
142 assert_eq!(e.dtype(), DataType::Boolean);
143 let f = to_any!(&[255, 0], DataType::Binary);
144 assert_eq!(f, AnyValue::Binary(&[255, 0]));
145 assert_eq!(f.dtype(), DataType::Binary);
146 }
147}