-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeclare_df_test.rs
110 lines (89 loc) · 3.46 KB
/
declare_df_test.rs
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
//! Df test case
use ::fabrix_core::{datetime, fx, series, uuid, Uuid};
/*
Create a dataframe with specified index column
cargo test --package fabrix --test declare_df_test -- test_new_df --exact --nocapture
*/
#[test]
fn test_new_df() {
let result_df = fx![
"ord";
"names" => ["Jacob", "Sam", "James", "Lucas", "Mia", "Livia"],
"ord" => [10,11,12,20,22,31],
"val" => [Some(10.1), None, Some(8.0), Some(9.5), Some(10.8), Some(11.2)],
"note" => [Some("FS"), Some("OP"), Some("TEC"), None, Some("SS"), None],
"dt" => [
datetime!(2016, 1, 8, 9, 10, 11),
datetime!(2017, 1, 7, 9, 10, 11),
datetime!(2018, 1, 6, 9, 10, 11),
datetime!(2019, 1, 5, 9, 10, 11),
datetime!(2020, 1, 4, 9, 10, 11),
datetime!(2020, 1, 3, 9, 10, 11),
]
];
assert!(result_df.is_ok());
let df = result_df.unwrap();
assert_eq!(df.shape(), (6, 5));
}
/*
query data from df
cargo test --package fabrix --test declare_df_test -- test_df_query --exact --nocapture
*/
#[test]
fn test_df_query() {
let df = fx![
"ord";
"names" => ["Jacob", "Sam", "James", "Lucas", "Mia", "Livia"],
"ord" => [10,11,12,20,22,31],
"val" => [Some(10.1), None, Some(8.0), Some(9.5), Some(10.8), Some(11.2)],
"note" => [Some("FS"), Some("OP"), Some("TEC"), None, Some("SS"), None],
"dt" => [
datetime!(2016, 1, 8, 9, 10, 11),
datetime!(2017, 1, 7, 9, 10, 11),
datetime!(2018, 1, 6, 9, 10, 11),
datetime!(2019, 1, 5, 9, 10, 11),
datetime!(2020, 1, 4, 9, 10, 11),
datetime!(2020, 1, 3, 9, 10, 11),
]
]
.unwrap();
println!("{:?}", df.get_columns(["names", "val"]).unwrap());
println!("{:?}", df.take_cols(["names", "val"]).unwrap());
println!("{:?}", df.take_rows_by_idx(&[0, 2]));
let flt = series!([12, 31]);
println!("{:?}", df.take_rows(&flt));
println!("{:?}", flt);
}
/*
The purpose of this test is to check Series consisted by custom-typed value can be taken by index.
Instead of using `series.take` method, which occurs `not implement` panic, use `series.take_iter` method.
*/
#[test]
fn test_obj_chunk() {
use polars::prelude::{IntoSeries, ObjectChunked};
let uuids = vec![uuid!(), uuid!(), uuid!(), uuid!(), uuid!(), uuid!()];
let arr = ObjectChunked::<Uuid>::new_from_vec("uuids", uuids);
let s = arr.into_series();
let mut it = vec![0, 2].into_iter();
println!("{:?}", s.take_iter(&mut it));
}
/*
Continue from the above test case, a chunkedArray of custom-typed value can be taken by index as well.
But explicit type annotation is required for polars `TakeIdx` enum.
*/
#[test]
fn test_obj_chunked_arr_take() {
// use std::array::IntoIter as ArrayIntoIter;
use std::vec::IntoIter as VecIntoIter;
use polars::prelude::{ChunkTake, ChunkedArray, NewChunkedArray, ObjectType, TakeIdx};
let uuids = [uuid!(), uuid!(), uuid!(), uuid!(), uuid!(), uuid!()];
let arr = ChunkedArray::<ObjectType<Uuid>>::from_slice("uuids", &uuids);
println!("{:?}", arr);
// TODO: update?
// let ti = [0usize, 2].into_iter();
// let tk: TakeIdx<ArrayIntoIter<usize, 2>, ArrayIntoIter<Option<usize>, 2>> = TakeIdx::Iter(ti);
// println!("{:?}", arr.take(tk));
let ti = vec![0usize, 4].into_iter();
let tk: TakeIdx<VecIntoIter<usize>, VecIntoIter<Option<usize>>> = TakeIdx::Iter(ti);
println!("{:?}", arr.take(tk));
}