Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-ops/src/chunked_array/list/dispersion.rs
6939 views
1
use arrow::temporal_conversions::MICROSECONDS_IN_DAY as US_IN_DAY;
2
3
use super::*;
4
5
pub(super) fn median_with_nulls(ca: &ListChunked) -> Series {
6
match ca.inner_dtype() {
7
DataType::Float32 => {
8
let out: Float32Chunked = ca
9
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))
10
.with_name(ca.name().clone());
11
out.into_series()
12
},
13
#[cfg(feature = "dtype-datetime")]
14
DataType::Date => {
15
let out: Int64Chunked = ca
16
.apply_amortized_generic(|s| {
17
s.and_then(|s| s.as_ref().median().map(|v| (v * (US_IN_DAY as f64)) as i64))
18
})
19
.with_name(ca.name().clone());
20
out.into_datetime(TimeUnit::Microseconds, None)
21
.into_series()
22
},
23
dt if dt.is_temporal() => {
24
let out: Int64Chunked = ca
25
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))
26
.with_name(ca.name().clone());
27
out.cast(dt).unwrap()
28
},
29
_ => {
30
let out: Float64Chunked = ca
31
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))
32
.with_name(ca.name().clone());
33
out.into_series()
34
},
35
}
36
}
37
38
pub(super) fn std_with_nulls(ca: &ListChunked, ddof: u8) -> Series {
39
match ca.inner_dtype() {
40
DataType::Float32 => {
41
let out: Float32Chunked = ca
42
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))
43
.with_name(ca.name().clone());
44
out.into_series()
45
},
46
#[cfg(feature = "dtype-duration")]
47
DataType::Duration(tu) => {
48
let out: Int64Chunked = ca
49
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))
50
.with_name(ca.name().clone());
51
out.into_duration(*tu).into_series()
52
},
53
_ => {
54
let out: Float64Chunked = ca
55
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof)))
56
.with_name(ca.name().clone());
57
out.into_series()
58
},
59
}
60
}
61
62
pub(super) fn var_with_nulls(ca: &ListChunked, ddof: u8) -> PolarsResult<Series> {
63
match ca.inner_dtype() {
64
DataType::Float32 => {
65
let out: Float32Chunked = ca
66
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))
67
.with_name(ca.name().clone());
68
Ok(out.into_series())
69
},
70
dt if dt.is_temporal() => {
71
polars_bail!(InvalidOperation: "variance of type {dt} is not supported")
72
},
73
_ => {
74
let out: Float64Chunked = ca
75
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))
76
.with_name(ca.name().clone());
77
Ok(out.into_series())
78
},
79
}
80
}
81
82