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/array/dispersion.rs
6939 views
1
use super::*;
2
3
pub(super) fn mean_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {
4
let mut out = match ca.inner_dtype() {
5
DataType::Float32 => {
6
let out: Float32Chunked = ca
7
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean().map(|v| v as f32)))
8
.with_name(ca.name().clone());
9
out.into_series()
10
},
11
#[cfg(feature = "dtype-duration")]
12
DataType::Duration(tu) => {
13
let out: Int64Chunked = ca
14
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean().map(|v| v as i64)))
15
.with_name(ca.name().clone());
16
out.into_duration(*tu).into_series()
17
},
18
_ => {
19
let out: Float64Chunked = ca
20
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean()))
21
.with_name(ca.name().clone());
22
out.into_series()
23
},
24
};
25
out.rename(ca.name().clone());
26
Ok(out)
27
}
28
29
pub(super) fn median_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {
30
let mut out = match ca.inner_dtype() {
31
DataType::Float32 => {
32
let out: Float32Chunked = ca
33
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))
34
.with_name(ca.name().clone());
35
out.into_series()
36
},
37
#[cfg(feature = "dtype-duration")]
38
DataType::Duration(tu) => {
39
let out: Int64Chunked = ca
40
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))
41
.with_name(ca.name().clone());
42
out.into_duration(*tu).into_series()
43
},
44
_ => {
45
let out: Float64Chunked = ca
46
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))
47
.with_name(ca.name().clone());
48
out.into_series()
49
},
50
};
51
out.rename(ca.name().clone());
52
Ok(out)
53
}
54
55
pub(super) fn std_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {
56
let mut out = match ca.inner_dtype() {
57
DataType::Float32 => {
58
let out: Float32Chunked = ca
59
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))
60
.with_name(ca.name().clone());
61
out.into_series()
62
},
63
#[cfg(feature = "dtype-duration")]
64
DataType::Duration(tu) => {
65
let out: Int64Chunked = ca
66
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))
67
.with_name(ca.name().clone());
68
out.into_duration(*tu).into_series()
69
},
70
_ => {
71
let out: Float64Chunked = {
72
ca.amortized_iter()
73
.map(|s| s.and_then(|s| s.as_ref().std(ddof)))
74
.collect()
75
};
76
out.into_series()
77
},
78
};
79
out.rename(ca.name().clone());
80
Ok(out)
81
}
82
83
pub(super) fn var_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {
84
let mut out = match ca.inner_dtype() {
85
DataType::Float32 => {
86
let out: Float32Chunked = ca
87
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))
88
.with_name(ca.name().clone());
89
out.into_series()
90
},
91
#[cfg(feature = "dtype-duration")]
92
DataType::Duration(TimeUnit::Milliseconds) => {
93
let out: Int64Chunked = ca
94
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
95
.with_name(ca.name().clone());
96
out.into_duration(TimeUnit::Milliseconds).into_series()
97
},
98
#[cfg(feature = "dtype-duration")]
99
DataType::Duration(TimeUnit::Microseconds | TimeUnit::Nanoseconds) => {
100
let out: Int64Chunked = ca
101
.cast(&DataType::Array(
102
Box::new(DataType::Duration(TimeUnit::Milliseconds)),
103
ca.width(),
104
))
105
.unwrap()
106
.array()
107
.unwrap()
108
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
109
.with_name(ca.name().clone());
110
out.into_duration(TimeUnit::Milliseconds).into_series()
111
},
112
_ => {
113
let out: Float64Chunked = ca
114
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))
115
.with_name(ca.name().clone());
116
out.into_series()
117
},
118
};
119
out.rename(ca.name().clone());
120
Ok(out)
121
}
122
123