polars_core/chunked_array/logical/
duration.rs1use super::*;
2use crate::prelude::*;
3
4pub type DurationChunked = Logical<DurationType, Int64Type>;
5
6impl Int64Chunked {
7 pub fn into_duration(self, timeunit: TimeUnit) -> DurationChunked {
8 let mut dt = DurationChunked::new_logical(self);
9 dt.2 = Some(DataType::Duration(timeunit));
10 dt
11 }
12}
13
14impl LogicalType for DurationChunked {
15 fn dtype(&self) -> &DataType {
16 self.2.as_ref().unwrap()
17 }
18
19 fn get_any_value(&self, i: usize) -> PolarsResult<AnyValue<'_>> {
20 self.0
21 .get_any_value(i)
22 .map(|av| av.as_duration(self.time_unit()))
23 }
24 unsafe fn get_any_value_unchecked(&self, i: usize) -> AnyValue<'_> {
25 self.0
26 .get_any_value_unchecked(i)
27 .as_duration(self.time_unit())
28 }
29
30 fn cast_with_options(
31 &self,
32 dtype: &DataType,
33 cast_options: CastOptions,
34 ) -> PolarsResult<Series> {
35 use DataType::*;
36 use TimeUnit::*;
37 match dtype {
38 Duration(tu) => {
39 let to_unit = *tu;
40 let out = match (self.time_unit(), to_unit) {
41 (Milliseconds, Microseconds) => self.0.as_ref() * 1_000i64,
42 (Milliseconds, Nanoseconds) => self.0.as_ref() * 1_000_000i64,
43 (Microseconds, Milliseconds) => {
44 self.0.as_ref().wrapping_trunc_div_scalar(1_000i64)
45 },
46 (Microseconds, Nanoseconds) => self.0.as_ref() * 1_000i64,
47 (Nanoseconds, Milliseconds) => {
48 self.0.as_ref().wrapping_trunc_div_scalar(1_000_000i64)
49 },
50 (Nanoseconds, Microseconds) => {
51 self.0.as_ref().wrapping_trunc_div_scalar(1_000i64)
52 },
53 _ => return Ok(self.clone().into_series()),
54 };
55 Ok(out.into_duration(to_unit).into_series())
56 },
57 dt if dt.is_primitive_numeric() => self.0.cast_with_options(dtype, cast_options),
58 dt => {
59 polars_bail!(
60 InvalidOperation:
61 "casting from {:?} to {:?} not supported",
62 self.dtype(), dt
63 )
64 },
65 }
66 }
67}