Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/aexpr/function_expr/business.rs
7889 views
1
use std::fmt::{Display, Formatter};
2
3
use polars_core::prelude::*;
4
use polars_ops::prelude::Roll;
5
6
use super::FunctionOptions;
7
use crate::plans::aexpr::function_expr::FieldsMapper;
8
use crate::prelude::FunctionFlags;
9
10
#[cfg_attr(feature = "ir_serde", derive(serde::Serialize, serde::Deserialize))]
11
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
12
pub enum IRBusinessFunction {
13
BusinessDayCount {
14
week_mask: [bool; 7],
15
holidays: Vec<i32>,
16
},
17
AddBusinessDay {
18
week_mask: [bool; 7],
19
holidays: Vec<i32>,
20
roll: Roll,
21
},
22
IsBusinessDay {
23
week_mask: [bool; 7],
24
holidays: Vec<i32>,
25
},
26
}
27
28
impl IRBusinessFunction {
29
pub fn get_field(&self, mapper: FieldsMapper) -> PolarsResult<Field> {
30
match self {
31
Self::BusinessDayCount { .. } => mapper.with_dtype(DataType::Int32),
32
Self::AddBusinessDay { .. } => mapper.with_same_dtype(),
33
Self::IsBusinessDay { .. } => mapper.with_dtype(DataType::Boolean),
34
}
35
}
36
pub fn function_options(&self) -> FunctionOptions {
37
use IRBusinessFunction as B;
38
match self {
39
B::BusinessDayCount { .. } => {
40
FunctionOptions::elementwise().with_flags(|f| f | FunctionFlags::ALLOW_RENAME)
41
},
42
B::AddBusinessDay { .. } | B::IsBusinessDay { .. } => FunctionOptions::elementwise(),
43
}
44
}
45
}
46
47
impl Display for IRBusinessFunction {
48
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49
use IRBusinessFunction::*;
50
let s = match self {
51
BusinessDayCount { .. } => "business_day_count",
52
AddBusinessDay { .. } => "add_business_days",
53
IsBusinessDay { .. } => "is_business_day",
54
};
55
write!(f, "{s}")
56
}
57
}
58
59