polars_core/chunked_array/logical/
date.rs

1use super::*;
2use crate::prelude::*;
3pub type DateChunked = Logical<DateType, Int32Type>;
4
5impl Int32Chunked {
6    pub fn into_date(self) -> DateChunked {
7        // SAFETY: no invalid states.
8        unsafe { DateChunked::new_logical(self, DataType::Date) }
9    }
10}
11
12impl LogicalType for DateChunked {
13    fn dtype(&self) -> &DataType {
14        &DataType::Date
15    }
16
17    fn get_any_value(&self, i: usize) -> PolarsResult<AnyValue<'_>> {
18        self.phys.get_any_value(i).map(|av| av.as_date())
19    }
20
21    unsafe fn get_any_value_unchecked(&self, i: usize) -> AnyValue<'_> {
22        self.phys.get_any_value_unchecked(i).as_date()
23    }
24
25    fn cast_with_options(
26        &self,
27        dtype: &DataType,
28        cast_options: CastOptions,
29    ) -> PolarsResult<Series> {
30        use DataType::*;
31        match dtype {
32            Date => Ok(self.clone().into_series()),
33            #[cfg(feature = "dtype-datetime")]
34            Datetime(tu, tz) => {
35                let casted = self.phys.cast_with_options(dtype, cast_options)?;
36                let casted = casted.datetime().unwrap();
37                let conversion = match tu {
38                    TimeUnit::Nanoseconds => NS_IN_DAY,
39                    TimeUnit::Microseconds => US_IN_DAY,
40                    TimeUnit::Milliseconds => MS_IN_DAY,
41                };
42                Ok(casted
43                    .physical()
44                    .checked_mul_scalar(conversion)
45                    .into_datetime(*tu, tz.clone())
46                    .into_series())
47            },
48            dt if dt.is_primitive_numeric() => self.phys.cast_with_options(dtype, cast_options),
49            dt => {
50                polars_bail!(
51                    InvalidOperation:
52                    "casting from {:?} to {:?} not supported",
53                    self.dtype(), dt
54                )
55            },
56        }
57    }
58}