Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-lazy/src/tests/arity.rs
6939 views
1
use super::*;
2
3
#[test]
4
#[cfg(feature = "cov")]
5
fn test_pearson_corr() -> PolarsResult<()> {
6
let df = df! {
7
"uid" => [0, 0, 0, 1, 1, 1],
8
"day" => [1, 2, 4, 1, 2, 3],
9
"cumcases" => [10, 12, 15, 25, 30, 41]
10
}
11
.unwrap();
12
13
let out = df
14
.clone()
15
.lazy()
16
.group_by_stable([col("uid")])
17
// a double aggregation expression.
18
.agg([pearson_corr(col("day"), col("cumcases")).alias("pearson_corr")])
19
.collect()?;
20
let s = out.column("pearson_corr")?.f64()?;
21
assert!((s.get(0).unwrap() - 0.997176).abs() < 0.000001);
22
assert!((s.get(1).unwrap() - 0.977356).abs() < 0.000001);
23
24
let out = df
25
.lazy()
26
.group_by_stable([col("uid")])
27
// a double aggregation expression.
28
.agg([pearson_corr(col("day"), col("cumcases"))
29
.pow(2.0)
30
.alias("pearson_corr")])
31
.collect()
32
.unwrap();
33
let s = out.column("pearson_corr")?.f64()?;
34
assert!((s.get(0).unwrap() - 0.994360902255639).abs() < 0.000001);
35
assert!((s.get(1).unwrap() - 0.9552238805970149).abs() < 0.000001);
36
Ok(())
37
}
38
39
// TODO! fix this we must get a token that prevents resetting the string cache until the plan has
40
// finished running. We cannot store a mutexguard in the executionstate because they don't implement
41
// send.
42
// #[test]
43
// fn test_single_thread_when_then_otherwise_categorical() -> PolarsResult<()> {
44
// let df = df!["col1"=> ["a", "b", "a", "b"],
45
// "col2"=> ["a", "a", "b", "b"],
46
// "col3"=> ["same", "same", "same", "same"]
47
// ]?;
48
49
// let out = df
50
// .lazy()
51
// .with_column(col("*").cast(DataType::Categorical))
52
// .select([when(col("col1").eq(col("col2")))
53
// .then(col("col3"))
54
// .otherwise(col("col1"))])
55
// .collect()?;
56
// let col = out.column("col3")?;
57
// assert_eq!(col.dtype(), &DataType::Categorical);
58
// let s = format!("{}", col);
59
// assert!(s.contains("same"));
60
// Ok(())
61
// }
62
63
#[test]
64
fn test_lazy_ternary() {
65
let df = get_df()
66
.lazy()
67
.with_column(
68
when(col("sepal_length").lt(lit(5.0)))
69
.then(lit(10))
70
.otherwise(lit(1))
71
.alias("new"),
72
)
73
.collect()
74
.unwrap();
75
assert_eq!(
76
43,
77
df.column("new")
78
.unwrap()
79
.as_materialized_series()
80
.sum::<i32>()
81
.unwrap()
82
);
83
}
84
85