Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-ops/src/series/ops/duration.rs
6939 views
1
use arrow::temporal_conversions::{MICROSECONDS, MILLISECONDS, NANOSECONDS, SECONDS_IN_DAY};
2
use polars_core::datatypes::{AnyValue, DataType, TimeUnit};
3
use polars_core::prelude::Column;
4
use polars_error::PolarsResult;
5
6
pub fn impl_duration(s: &[Column], time_unit: TimeUnit) -> PolarsResult<Column> {
7
if s.iter().any(|s| s.is_empty()) {
8
return Ok(Column::new_empty(
9
s[0].name().clone(),
10
&DataType::Duration(time_unit),
11
));
12
}
13
14
// TODO: Handle overflow for UInt64
15
let weeks = &s[0];
16
let days = &s[1];
17
let hours = &s[2];
18
let minutes = &s[3];
19
let seconds = &s[4];
20
let mut milliseconds = s[5].clone();
21
let mut microseconds = s[6].clone();
22
let mut nanoseconds = s[7].clone();
23
24
let is_scalar = |s: &Column| s.len() == 1;
25
let is_zero_scalar = |s: &Column| is_scalar(s) && s.get(0).unwrap() == AnyValue::Int64(0);
26
27
// Process subseconds
28
let max_len = s.iter().map(|s| s.len()).max().unwrap();
29
let mut duration = match time_unit {
30
TimeUnit::Microseconds => {
31
if is_scalar(&microseconds) {
32
microseconds = microseconds.new_from_index(0, max_len);
33
}
34
if !is_zero_scalar(&nanoseconds) {
35
microseconds = (microseconds + (nanoseconds.wrapping_trunc_div_scalar(1_000)))?;
36
}
37
if !is_zero_scalar(&milliseconds) {
38
microseconds = (microseconds + milliseconds * 1_000)?;
39
}
40
microseconds
41
},
42
TimeUnit::Nanoseconds => {
43
if is_scalar(&nanoseconds) {
44
nanoseconds = nanoseconds.new_from_index(0, max_len);
45
}
46
if !is_zero_scalar(&microseconds) {
47
nanoseconds = (nanoseconds + microseconds * 1_000)?;
48
}
49
if !is_zero_scalar(&milliseconds) {
50
nanoseconds = (nanoseconds + milliseconds * 1_000_000)?;
51
}
52
nanoseconds
53
},
54
TimeUnit::Milliseconds => {
55
if is_scalar(&milliseconds) {
56
milliseconds = milliseconds.new_from_index(0, max_len);
57
}
58
if !is_zero_scalar(&nanoseconds) {
59
milliseconds = (milliseconds + (nanoseconds.wrapping_trunc_div_scalar(1_000_000)))?;
60
}
61
if !is_zero_scalar(&microseconds) {
62
milliseconds = (milliseconds + (microseconds.wrapping_trunc_div_scalar(1_000)))?;
63
}
64
milliseconds
65
},
66
};
67
68
// Process other duration specifiers
69
let multiplier = match time_unit {
70
TimeUnit::Nanoseconds => NANOSECONDS,
71
TimeUnit::Microseconds => MICROSECONDS,
72
TimeUnit::Milliseconds => MILLISECONDS,
73
};
74
if !is_zero_scalar(seconds) {
75
duration = (duration + seconds * multiplier)?;
76
}
77
if !is_zero_scalar(minutes) {
78
duration = (duration + minutes * multiplier * 60)?;
79
}
80
if !is_zero_scalar(hours) {
81
duration = (duration + hours * multiplier * 60 * 60)?;
82
}
83
if !is_zero_scalar(days) {
84
duration = (duration + days * multiplier * SECONDS_IN_DAY)?;
85
}
86
if !is_zero_scalar(weeks) {
87
duration = (duration + weeks * multiplier * SECONDS_IN_DAY * 7)?;
88
}
89
90
duration.cast(&DataType::Duration(time_unit))
91
}
92
93