polars_core/chunked_array/logical/
date.rs

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