polars-rows-iter 0.12.0

Library for easy and convenient row iteration of polars dataframes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use itertools::Itertools;
use polars::prelude::*;
use rand::{
    distr::{Alphanumeric, Distribution, StandardUniform},
    rngs::StdRng,
    Rng, SeedableRng,
};
use std::collections::HashMap;

pub type IsOptional = bool;

const TIME64_MAX_VALUE: i64 = 24 * 60 * 60 * 1_000_000_000;

#[derive(Debug, Clone)]
pub struct ColumnType(pub DataType, pub IsOptional);

pub fn create_values<T, F>(height: usize, mut get_value: F) -> Vec<T>
where
    F: FnMut() -> T,
{
    let mut values = Vec::<T>::with_capacity(height);
    for _ in 0..height {
        values.push(get_value());
    }

    values
}

pub fn create_optional_bool(rng: &mut StdRng) -> Option<bool> {
    let is_none = rng.random_bool(0.5);
    if !is_none {
        Some(rng.random_bool(0.5))
    } else {
        None
    }
}

pub fn create_optional_number<T>(rng: &mut StdRng) -> Option<T>
where
    StandardUniform: Distribution<T>,
{
    let is_none = rng.random_bool(0.5);
    if !is_none {
        Some(rng.random())
    } else {
        None
    }
}

pub fn create_optional<T, F>(rng: &mut StdRng, mut create_value: F) -> Option<T>
where
    F: FnMut(&mut StdRng) -> T,
{
    let is_none = rng.random_bool(0.5);
    if !is_none {
        Some(create_value(rng))
    } else {
        None
    }
}

pub fn create_random_string(rng: &mut StdRng) -> String {
    let size: usize = rng.random_range(4..32);
    rng.sample_iter(&Alphanumeric).take(size).map(char::from).collect()
}

pub fn create_random_binary(rng: &mut StdRng) -> Vec<u8> {
    let size: usize = rng.random_range(4..32);
    rng.sample_iter(&Alphanumeric).take(size).collect()
}

pub fn create_enum_values<'a>(
    categories: &'a FrozenCategories,
    mapping: &'a CategoricalMapping,
    height: usize,
    rng: &mut StdRng,
) -> Vec<&'a str> {
    (0..height)
        .map(|_| {
            let enum_index = rng.random_range(0..categories.categories().len() as u32);
            mapping.cat_to_str(enum_index).unwrap()
        })
        .collect()
}

pub fn create_optional_enum_values<'a>(
    categories: &'a FrozenCategories,
    mapping: &'a CategoricalMapping,
    height: usize,
    rng: &mut StdRng,
) -> Vec<Option<&'a str>> {
    (0..height)
        .map(move |_| {
            let enum_index = rng.random_range(0..categories.categories().len() as u32);
            match rng.random_bool(0.5) {
                true => mapping.cat_to_str(enum_index),
                false => None,
            }
        })
        .collect()
}

pub fn create_column(name: &str, dtype: &DataType, optional: IsOptional, height: usize, rng: &mut StdRng) -> Column {
    // println!("Creating column {name} with type {dtype} (optional: {optional})");
    let name = name.into();
    match dtype {
        DataType::Boolean => match optional {
            true => Column::new(name, create_values(height, || create_optional_bool(rng))),
            false => Column::new(name, create_values(height, || rng.random_bool(0.5))),
        },
        DataType::UInt8 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<u8>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<u8>())),
        },
        DataType::UInt16 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<u16>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<u16>())),
        },
        DataType::UInt32 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<u32>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<u32>())),
        },
        DataType::UInt64 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<u64>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<u64>())),
        },
        DataType::Int8 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i8>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<i8>())),
        },
        DataType::Int16 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i16>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<i16>())),
        },
        DataType::Int32 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i32>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<i32>())),
        },
        DataType::Int64 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i64>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<i64>())),
        },
        DataType::Float32 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<f32>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<f32>())),
        },
        DataType::Float64 => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<f64>(rng))),
            false => Column::new(name, create_values(height, || rng.random::<f64>())),
        },
        DataType::String => match optional {
            true => Column::new(
                name,
                create_values(height, || create_optional(rng, create_random_string)),
            ),
            false => Column::new(name, create_values(height, || create_random_string(rng))),
        },
        DataType::Categorical(mapping, ordering) => match optional {
            true => Column::new(
                name,
                create_values(height, || create_optional(rng, create_random_string)),
            )
            .cast(&DataType::Categorical(mapping.clone(), ordering.clone()))
            .unwrap(),
            false => Column::new(name, create_values(height, || create_random_string(rng)))
                .cast(&DataType::Categorical(mapping.clone(), ordering.clone()))
                .unwrap(),
        },
        DataType::Enum(categories, mapping) => match optional {
            true => Column::new(
                name,
                create_optional_enum_values(categories.as_ref(), mapping.as_ref(), height, rng),
            )
            .cast(&DataType::Enum(categories.clone(), mapping.clone()))
            .unwrap(),
            false => Column::new(
                name,
                create_enum_values(categories.as_ref(), mapping.as_ref(), height, rng),
            )
            .cast(&DataType::Enum(categories.clone(), mapping.clone()))
            .unwrap(),
        },
        DataType::Datetime(unit, zone) => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i64>(rng)))
                .cast(&DataType::Datetime(*unit, zone.clone()))
                .unwrap(),
            false => Column::new(name, create_values(height, || rng.random::<i64>()))
                .cast(&DataType::Datetime(*unit, zone.clone()))
                .unwrap(),
        },
        DataType::Date => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i32>(rng)))
                .cast(&DataType::Date)
                .unwrap(),
            false => Column::new(name, create_values(height, || rng.random::<i32>()))
                .cast(&DataType::Date)
                .unwrap(),
        },
        DataType::Time => match optional {
            true => Column::new(
                name,
                create_values(height, || {
                    create_optional(rng, |rng| {
                        let value: i64 = rng.random_range(0..TIME64_MAX_VALUE);
                        value
                    })
                }),
            )
            .cast(&DataType::Time)
            .unwrap(),
            false => Column::new(
                name,
                create_values(height, || {
                    let value: i64 = rng.random_range(0..TIME64_MAX_VALUE);
                    value
                }),
            )
            .cast(&DataType::Time)
            .unwrap(),
        },
        DataType::Duration(unit) => match optional {
            true => Column::new(name, create_values(height, || create_optional_number::<i64>(rng)))
                .cast(&DataType::Duration(*unit))
                .unwrap(),
            false => Column::new(name, create_values(height, || rng.random::<i64>()))
                .cast(&DataType::Duration(*unit))
                .unwrap(),
        },
        DataType::Binary => match optional {
            true => Column::new(
                name,
                create_values(height, || create_optional(rng, create_random_binary)),
            ),
            false => Column::new(name, create_values(height, || create_random_binary(rng))),
        },
        DataType::BinaryOffset => match optional {
            true => {
                let values: LargeBinaryArray = create_values(height, || create_optional(rng, create_random_binary))
                    .into_iter()
                    .collect();

                BinaryOffsetChunked::from(values).into_column().with_name(name)
            }
            false => {
                let values: LargeBinaryArray = create_values(height, || Some(create_random_binary(rng)))
                    .into_iter()
                    .collect();

                BinaryOffsetChunked::from(values).into_column().with_name(name)
            }
        },
        DataType::List(dtype) => {
            if optional {
                let series = (0..height)
                    .map(|_| {
                        if rng.random_bool(0.5) {
                            Some(
                                create_column("", dtype.as_ref(), optional, height, rng)
                                    .as_materialized_series()
                                    .clone(),
                            )
                        } else {
                            None
                        }
                    })
                    .collect_vec();

                Column::new(name, series)
            } else {
                let series = (0..height)
                    .map(|_| {
                        create_column("", dtype.as_ref(), optional, height, rng)
                            .as_materialized_series()
                            .clone()
                    })
                    .collect_vec();

                Column::new(name, series)
            }
        }
        _ => todo!(),
    }
}

pub fn create_dataframe(columns: HashMap<&str, ColumnType>, height: usize) -> DataFrame {
    let mut rng = StdRng::seed_from_u64(0);
    let columns = columns
        .into_iter()
        .map(|(name, ColumnType(dtype, optional))| create_column(name, dtype.as_ref(), optional, height, &mut rng))
        .collect::<Vec<Column>>();

    DataFrame::new(columns).unwrap()
}

#[macro_export]
macro_rules! create_rows_iter_test_for_chunked_type {
    ($func_name:ident, $type:ty, $type_name:ident, $dtype:expr, $height:ident) => {
        #[test]
        fn $func_name() {
            let mut rng = StdRng::seed_from_u64(0);
            let height = $height;
            let dtype = $dtype;

            let col = create_column("col", &dtype, false, height, &mut rng);
            let col_opt = create_column("col_opt", &dtype, true, height, &mut rng);

            let col_values = col
                .as_series()
                .unwrap()
                .$type_name()
                .unwrap()
                .iter()
                .map(|v| v.unwrap())
                .collect_vec();

            let col_opt_values = col_opt
                .as_series()
                .unwrap()
                .$type_name()
                .unwrap()
                .iter()
                .collect_vec();

            let df = DataFrame::new(vec![col, col_opt]).unwrap();

            let col_iter = col_values.iter();
            let col_opt_iter = col_opt_values.iter();

            let expected_rows = izip!(col_iter, col_opt_iter)
                .map(|(&col, &col_opt)| TestRow { col, col_opt })
                .collect_vec();

            #[derive(Debug, FromDataFrameRow, PartialEq)]
            struct TestRow {
                col: $type,
                col_opt: Option<$type>,
            }

            let rows = df
                .rows_iter::<TestRow>()
                .unwrap()
                .map(|v| v.unwrap())
                .collect_vec();

            assert_eq!(rows, expected_rows)
        }
    };
}

#[macro_export]
macro_rules! create_rows_iter_test_for_logical_type {
    ($func_name:ident, $type:ty, $type_name:ident, $dtype:expr, $height:ident) => {
        #[test]
        fn $func_name() {
            let mut rng = StdRng::seed_from_u64(0);
            let height = $height;
            let dtype = $dtype;

            let col = create_column("col", &dtype, false, height, &mut rng);
            let col_opt = create_column("col_opt", &dtype, true, height, &mut rng);

            let col_values = col
                .as_series()
                .unwrap()
                .$type_name()
                .unwrap()
                .phys
                .iter()
                .map(|v| v.unwrap())
                .collect_vec();

            let col_opt_values = col_opt
                .as_series()
                .unwrap()
                .$type_name()
                .unwrap()
                .phys
                .iter()
                .collect_vec();

            let df = DataFrame::new(vec![col, col_opt]).unwrap();

            let col_iter = col_values.iter();
            let col_opt_iter = col_opt_values.iter();

            let expected_rows = izip!(col_iter, col_opt_iter)
                .map(|(&col, &col_opt)| TestRow { col, col_opt })
                .collect_vec();

            #[derive(Debug, FromDataFrameRow, PartialEq)]
            struct TestRow {
                col: $type,
                col_opt: Option<$type>,
            }

            let rows = df
                .rows_iter::<TestRow>()
                .unwrap()
                .map(|v| v.unwrap())
                .collect_vec();

            assert_eq!(rows, expected_rows)
        }
    };
}