Path: blob/main/crates/polars-time/src/chunkedarray/kernels.rs
8407 views
//! macros that define kernels for extracting1//! `week`, `weekday`, `year`, `hour` etc. from primitive arrays.2use arrow::array::{BooleanArray, PrimitiveArray};3#[cfg(feature = "dtype-time")]4use arrow::temporal_conversions::time64ns_to_time_opt;5use arrow::temporal_conversions::{6date32_to_datetime_opt, timestamp_ms_to_datetime_opt, timestamp_ns_to_datetime_opt,7timestamp_us_to_datetime_opt,8};9use chrono::{Datelike, Timelike};1011use super::super::windows::calendar::*;12use super::*;1314trait PolarsIso {15fn week(&self) -> i8;16fn iso_year(&self) -> i32;17}1819impl PolarsIso for NaiveDateTime {20fn week(&self) -> i8 {21self.iso_week().week().try_into().unwrap()22}23fn iso_year(&self) -> i32 {24self.iso_week().year()25}26}2728impl PolarsIso for NaiveDate {29fn week(&self) -> i8 {30self.iso_week().week().try_into().unwrap()31}32fn iso_year(&self) -> i32 {33self.iso_week().year()34}35}3637macro_rules! to_temporal_unit {38($name: ident, $chrono_method: ident, $to_datetime_fn: expr,39$primitive_in: ty,40$primitive_out: ty,41$dtype_out:expr) => {42pub(crate) fn $name(arr: &PrimitiveArray<$primitive_in>) -> ArrayRef {43Box::new(PrimitiveArray::<$primitive_out>::from_trusted_len_iter(44arr.iter().map(|opt_value| {45opt_value.and_then(|&value| {46$to_datetime_fn(value).map(|dt| dt.$chrono_method() as $primitive_out)47})48}),49)) as ArrayRef50}51};52}5354macro_rules! to_boolean_temporal_unit {55($name: ident, $chrono_method: ident, $boolean_method: ident, $to_datetime_fn: expr, $dtype_in: ty) => {56pub(crate) fn $name(arr: &PrimitiveArray<$dtype_in>) -> ArrayRef {57Box::new(BooleanArray::from_trusted_len_iter(arr.iter().map(58|opt_value| {59opt_value.and_then(|&value| {60$to_datetime_fn(value).map(|dt| $boolean_method(dt.$chrono_method()))61})62},63)))64}65};66}6768macro_rules! to_calendar_value {69($name: ident, $dt: ident, $expr: expr, $to_datetime_fn: expr,70$primitive_in: ty,71$primitive_out: ty,72$dtype_out:expr) => {73pub(crate) fn $name(arr: &PrimitiveArray<$primitive_in>) -> ArrayRef {74Box::new(PrimitiveArray::<$primitive_out>::from_trusted_len_iter(75arr.iter().map(|opt_value| {76opt_value.and_then(|&value| {77$to_datetime_fn(value).map(|$dt| $expr as $primitive_out)78})79}),80)) as ArrayRef81}82};83}8485// Dates86#[cfg(feature = "dtype-date")]87to_temporal_unit!(88date_to_iso_week,89week,90date32_to_datetime_opt,91i32,92i8,93ArrowDataType::Int894);95#[cfg(feature = "dtype-date")]96to_temporal_unit!(97date_to_iso_year,98iso_year,99date32_to_datetime_opt,100i32,101i32,102ArrowDataType::Int32103);104#[cfg(feature = "dtype-date")]105to_temporal_unit!(106date_to_year,107year,108date32_to_datetime_opt,109i32,110i32,111ArrowDataType::Int32112);113#[cfg(feature = "dtype-date")]114to_boolean_temporal_unit!(115date_to_is_leap_year,116year,117is_leap_year,118date32_to_datetime_opt,119i32120);121#[cfg(feature = "dtype-date")]122to_temporal_unit!(123date_to_month,124month,125date32_to_datetime_opt,126i32,127i8,128ArrowDataType::Int8129);130#[cfg(feature = "dtype-date")]131to_temporal_unit!(132date_to_day,133day,134date32_to_datetime_opt,135i32,136i8,137ArrowDataType::Int8138);139#[cfg(feature = "dtype-date")]140to_temporal_unit!(141date_to_ordinal,142ordinal,143date32_to_datetime_opt,144i32,145i16,146ArrowDataType::Int16147);148#[cfg(feature = "dtype-date")]149to_calendar_value!(150date_to_days_in_month,151dt,152days_in_month(dt.year(), dt.month() as u8),153date32_to_datetime_opt,154i32,155i8,156ArrowDataType::Int8157);158159// Times160#[cfg(feature = "dtype-time")]161to_temporal_unit!(162time_to_hour,163hour,164time64ns_to_time_opt,165i64,166i8,167ArrowDataType::Int8168);169#[cfg(feature = "dtype-time")]170to_temporal_unit!(171time_to_minute,172minute,173time64ns_to_time_opt,174i64,175i8,176ArrowDataType::Int8177);178#[cfg(feature = "dtype-time")]179to_temporal_unit!(180time_to_second,181second,182time64ns_to_time_opt,183i64,184i8,185ArrowDataType::Int8186);187#[cfg(feature = "dtype-time")]188to_temporal_unit!(189time_to_nanosecond,190nanosecond,191time64ns_to_time_opt,192i64,193i32,194ArrowDataType::Int32195);196197#[cfg(feature = "dtype-datetime")]198to_temporal_unit!(199datetime_to_ordinal_ns,200ordinal,201timestamp_ns_to_datetime_opt,202i64,203i16,204ArrowDataType::Int16205);206207#[cfg(feature = "dtype-datetime")]208to_temporal_unit!(209datetime_to_ordinal_ms,210ordinal,211timestamp_ms_to_datetime_opt,212i64,213i16,214ArrowDataType::Int16215);216#[cfg(feature = "dtype-datetime")]217to_temporal_unit!(218datetime_to_ordinal_us,219ordinal,220timestamp_us_to_datetime_opt,221i64,222i16,223ArrowDataType::Int16224);225226#[cfg(feature = "dtype-datetime")]227to_temporal_unit!(228datetime_to_iso_year_ns,229iso_year,230timestamp_ns_to_datetime_opt,231i64,232i32,233ArrowDataType::Int32234);235236#[cfg(feature = "dtype-datetime")]237to_temporal_unit!(238datetime_to_iso_year_us,239iso_year,240timestamp_us_to_datetime_opt,241i64,242i32,243ArrowDataType::Int32244);245246#[cfg(feature = "dtype-datetime")]247to_temporal_unit!(248datetime_to_iso_year_ms,249iso_year,250timestamp_ms_to_datetime_opt,251i64,252i32,253ArrowDataType::Int32254);255#[cfg(feature = "dtype-datetime")]256to_boolean_temporal_unit!(257datetime_to_is_leap_year_ns,258year,259is_leap_year,260timestamp_ns_to_datetime_opt,261i64262);263#[cfg(feature = "dtype-datetime")]264to_boolean_temporal_unit!(265datetime_to_is_leap_year_us,266year,267is_leap_year,268timestamp_us_to_datetime_opt,269i64270);271#[cfg(feature = "dtype-datetime")]272to_boolean_temporal_unit!(273datetime_to_is_leap_year_ms,274year,275is_leap_year,276timestamp_ms_to_datetime_opt,277i64278);279280#[cfg(feature = "dtype-datetime")]281to_calendar_value!(282datetime_to_days_in_month_ns,283dt,284days_in_month(dt.year(), dt.month() as u8),285timestamp_ns_to_datetime_opt,286i64,287i8,288ArrowDataType::Int8289);290#[cfg(feature = "dtype-datetime")]291to_calendar_value!(292datetime_to_days_in_month_us,293dt,294days_in_month(dt.year(), dt.month() as u8),295timestamp_us_to_datetime_opt,296i64,297i8,298ArrowDataType::Int8299);300#[cfg(feature = "dtype-datetime")]301to_calendar_value!(302datetime_to_days_in_month_ms,303dt,304days_in_month(dt.year(), dt.month() as u8),305timestamp_ms_to_datetime_opt,306i64,307i8,308ArrowDataType::Int8309);310311312