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
8395 views
1
use num_traits::FromPrimitive;
2
use polars_utils::float16::pf16;
3
4
use super::*;
5
6
pub(super) fn mean_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {
7
let mut out = match ca.inner_dtype() {
8
#[cfg(feature = "dtype-f16")]
9
DataType::Float16 => {
10
let out: Float16Chunked = ca
11
.apply_amortized_generic(|s| {
12
s.and_then(|s| s.as_ref().mean().map(|v| pf16::from_f64(v).unwrap()))
13
})
14
.with_name(ca.name().clone());
15
out.into_series()
16
},
17
DataType::Float32 => {
18
let out: Float32Chunked = ca
19
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean().map(|v| v as f32)))
20
.with_name(ca.name().clone());
21
out.into_series()
22
},
23
#[cfg(feature = "dtype-duration")]
24
DataType::Duration(tu) => {
25
let out: Int64Chunked = ca
26
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean().map(|v| v as i64)))
27
.with_name(ca.name().clone());
28
out.into_duration(*tu).into_series()
29
},
30
_ => {
31
let out: Float64Chunked = ca
32
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean()))
33
.with_name(ca.name().clone());
34
out.into_series()
35
},
36
};
37
out.rename(ca.name().clone());
38
Ok(out)
39
}
40
41
pub(super) fn median_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {
42
let mut out = match ca.inner_dtype() {
43
#[cfg(feature = "dtype-f16")]
44
DataType::Float16 => {
45
let out: Float16Chunked = ca
46
.apply_amortized_generic(|s| {
47
s.and_then(|s| s.as_ref().median().map(|v| pf16::from_f64(v).unwrap()))
48
})
49
.with_name(ca.name().clone());
50
out.into_series()
51
},
52
DataType::Float32 => {
53
let out: Float32Chunked = ca
54
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))
55
.with_name(ca.name().clone());
56
out.into_series()
57
},
58
#[cfg(feature = "dtype-duration")]
59
DataType::Duration(tu) => {
60
let out: Int64Chunked = ca
61
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))
62
.with_name(ca.name().clone());
63
out.into_duration(*tu).into_series()
64
},
65
_ => {
66
let out: Float64Chunked = ca
67
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))
68
.with_name(ca.name().clone());
69
out.into_series()
70
},
71
};
72
out.rename(ca.name().clone());
73
Ok(out)
74
}
75
76
pub(super) fn std_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {
77
let mut out = match ca.inner_dtype() {
78
#[cfg(feature = "dtype-f16")]
79
DataType::Float16 => {
80
let out: Float16Chunked = ca
81
.apply_amortized_generic(|s| {
82
s.and_then(|s| s.as_ref().std(ddof).map(|v| pf16::from_f64(v).unwrap()))
83
})
84
.with_name(ca.name().clone());
85
out.into_series()
86
},
87
DataType::Float32 => {
88
let out: Float32Chunked = ca
89
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))
90
.with_name(ca.name().clone());
91
out.into_series()
92
},
93
#[cfg(feature = "dtype-duration")]
94
DataType::Duration(tu) => {
95
let out: Int64Chunked = ca
96
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))
97
.with_name(ca.name().clone());
98
out.into_duration(*tu).into_series()
99
},
100
_ => {
101
let out: Float64Chunked = {
102
ca.amortized_iter()
103
.map(|s| s.and_then(|s| s.as_ref().std(ddof)))
104
.collect()
105
};
106
out.into_series()
107
},
108
};
109
out.rename(ca.name().clone());
110
Ok(out)
111
}
112
113
pub(super) fn var_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {
114
let mut out = match ca.inner_dtype() {
115
#[cfg(feature = "dtype-f16")]
116
DataType::Float16 => {
117
let out: Float16Chunked = ca
118
.apply_amortized_generic(|s| {
119
s.and_then(|s| s.as_ref().var(ddof).map(|v| pf16::from_f64(v).unwrap()))
120
})
121
.with_name(ca.name().clone());
122
out.into_series()
123
},
124
DataType::Float32 => {
125
let out: Float32Chunked = ca
126
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))
127
.with_name(ca.name().clone());
128
out.into_series()
129
},
130
#[cfg(feature = "dtype-duration")]
131
DataType::Duration(TimeUnit::Milliseconds) => {
132
let out: Int64Chunked = ca
133
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
134
.with_name(ca.name().clone());
135
out.into_duration(TimeUnit::Milliseconds).into_series()
136
},
137
#[cfg(feature = "dtype-duration")]
138
DataType::Duration(TimeUnit::Microseconds | TimeUnit::Nanoseconds) => {
139
let out: Int64Chunked = ca
140
.cast(&DataType::Array(
141
Box::new(DataType::Duration(TimeUnit::Milliseconds)),
142
ca.width(),
143
))
144
.unwrap()
145
.array()
146
.unwrap()
147
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
148
.with_name(ca.name().clone());
149
out.into_duration(TimeUnit::Milliseconds).into_series()
150
},
151
_ => {
152
let out: Float64Chunked = ca
153
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))
154
.with_name(ca.name().clone());
155
out.into_series()
156
},
157
};
158
out.rename(ca.name().clone());
159
Ok(out)
160
}
161
162