Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-core/src/scalar/reduce.rs
6940 views
1
use crate::datatypes::AnyValue;
2
use crate::datatypes::time_unit::TimeUnit;
3
#[cfg(feature = "dtype-date")]
4
use crate::prelude::US_IN_DAY;
5
use crate::prelude::{DataType, Scalar};
6
7
pub fn mean_reduce(value: Option<f64>, dtype: DataType) -> Scalar {
8
match dtype {
9
DataType::Float32 => {
10
let val = value.map(|m| m as f32);
11
Scalar::new(dtype, val.into())
12
},
13
dt if dt.is_primitive_numeric() || dt.is_decimal() || dt.is_bool() => {
14
Scalar::new(DataType::Float64, value.into())
15
},
16
#[cfg(feature = "dtype-date")]
17
DataType::Date => {
18
let val = value.map(|v| (v * US_IN_DAY as f64) as i64);
19
Scalar::new(DataType::Datetime(TimeUnit::Microseconds, None), val.into())
20
},
21
#[cfg(feature = "dtype-datetime")]
22
dt @ DataType::Datetime(_, _) => {
23
let val = value.map(|v| v as i64);
24
Scalar::new(dt, val.into())
25
},
26
#[cfg(feature = "dtype-duration")]
27
dt @ DataType::Duration(_) => {
28
let val = value.map(|v| v as i64);
29
Scalar::new(dt, val.into())
30
},
31
#[cfg(feature = "dtype-time")]
32
dt @ DataType::Time => {
33
let val = value.map(|v| v as i64);
34
Scalar::new(dt, val.into())
35
},
36
dt => Scalar::new(dt, AnyValue::Null),
37
}
38
}
39
40