Skip to content

Commit

Permalink
Add DataType::Time
Browse files Browse the repository at this point in the history
This adds a Logical Type Time to Series.
Arrow time32/time64 will be converted to
arrow time64(nanosecond) when converted to
polars.
  • Loading branch information
ritchie46 committed Oct 12, 2021
1 parent 3d99b45 commit f70d219
Show file tree
Hide file tree
Showing 38 changed files with 1,019 additions and 587 deletions.
6 changes: 4 additions & 2 deletions polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private = []
dtype-full = [
"dtype-date",
"dtype-datetime",
"dtype-time",
"dtype-i8",
"dtype-i16",
"dtype-u8",
Expand All @@ -116,8 +117,9 @@ dtype-slim = [
]

# opt-in datatypes for Series
dtype-date = ["polars-core/dtype-date", "polars-lazy/dtype-date"]
dtype-datetime= ["polars-core/dtype-datetime", "polars-lazy/dtype-datetime"]
dtype-date = ["polars-core/dtype-date", "polars-lazy/dtype-date", "polars-io/dtype-date"]
dtype-datetime= ["polars-core/dtype-datetime", "polars-lazy/dtype-datetime", "polars-io/dtype-datetime"]
dtype-time= ["polars-core/dtype-time", "polars-io/dtype-time"]
dtype-i8 = ["polars-core/dtype-i8", "polars-lazy/dtype-i8"]
dtype-i16 = ["polars-core/dtype-i16", "polars-lazy/dtype-i16"]
dtype-u8 = ["polars-core/dtype-u8", "polars-lazy/dtype-u8"]
Expand Down
1 change: 1 addition & 0 deletions polars/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ moment = []
# opt-in datatypes for Series
dtype-date = ["temporal"]
dtype-datetime= ["temporal"]
dtype-time = ["temporal"]
dtype-i8 = []
dtype-i16 = []
dtype-u8 = []
Expand Down
2 changes: 2 additions & 0 deletions polars/polars-core/src/chunked_array/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ fn cast_impl(name: &str, chunks: &[ArrayRef], dtype: &DataType) -> Result<Series
use DataType::*;
let out = match dtype {
Date | Datetime => out.into_date(),
#[cfg(feature = "dtype-time")]
Time => out.into_time(),
_ => out,
};

Expand Down
87 changes: 70 additions & 17 deletions polars/polars-core/src/chunked_array/kernels/temporal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::chunked_array::temporal::conversions_utils::*;
//! macros that define kernels for extracting
//! `week`, `weekday`, `year`, `hour` etc. from primitive arrays.
use crate::prelude::*;
use arrow::array::{ArrayRef, PrimitiveArray};
use arrow::compute::arity::unary;
#[cfg(feature = "dtype-time")]
use arrow::temporal_conversions::time64ns_to_time;
use arrow::temporal_conversions::{date32_to_datetime, timestamp_ms_to_datetime};
use chrono::{Datelike, NaiveDate, NaiveDateTime, Timelike};
use std::sync::Arc;

Expand Down Expand Up @@ -42,115 +46,164 @@ macro_rules! to_temporal_unit {
}
};
}
#[cfg(feature = "dtype-date")]
to_temporal_unit!(
date_to_week,
week,
date_as_datetime,
date32_to_datetime,
i32,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-date")]
to_temporal_unit!(
date_to_weekday,
p_weekday,
date_as_datetime,
date32_to_datetime,
i32,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-date")]
to_temporal_unit!(
date_to_year,
year,
date_as_datetime,
date32_to_datetime,
i32,
ArrowDataType::Int32
);
#[cfg(feature = "dtype-date")]
to_temporal_unit!(
date_to_month,
month,
date_as_datetime,
date32_to_datetime,
i32,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-date")]
to_temporal_unit!(
date_to_day,
day,
date_as_datetime,
date32_to_datetime,
i32,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-date")]
to_temporal_unit!(
date_to_ordinal,
ordinal,
date_as_datetime,
date32_to_datetime,
i32,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_week,
week,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_weekday,
p_weekday,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_year,
year,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::Int32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_month,
month,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_day,
day,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_hour,
hour,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_minute,
minute,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_second,
second,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_nanosecond,
nanosecond,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-datetime")]
to_temporal_unit!(
datetime_to_ordinal,
ordinal,
datetime_as_datetime,
timestamp_ms_to_datetime,
i64,
ArrowDataType::UInt32
);

#[cfg(feature = "dtype-time")]
to_temporal_unit!(
time_to_hour,
hour,
time64ns_to_time,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-time")]
to_temporal_unit!(
time_to_minute,
minute,
time64ns_to_time,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-time")]
to_temporal_unit!(
time_to_second,
second,
time64ns_to_time,
i64,
ArrowDataType::UInt32
);
#[cfg(feature = "dtype-time")]
to_temporal_unit!(
time_to_nanosecond,
nanosecond,
time64ns_to_time,
i64,
ArrowDataType::UInt32
);
3 changes: 2 additions & 1 deletion polars/polars-core/src/chunked_array/logical/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
mod date;
mod datetime;
mod time;

pub use {date::*, datetime::*};
pub use {date::*, datetime::*, time::*};

use crate::prelude::*;
use std::marker::PhantomData;
Expand Down
22 changes: 22 additions & 0 deletions polars/polars-core/src/chunked_array/logical/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use super::*;
use crate::prelude::*;

pub type TimeChunked = Logical<TimeType, Int64Type>;

impl From<Int64Chunked> for TimeChunked {
fn from(ca: Int64Chunked) -> Self {
TimeChunked::new(ca)
}
}

impl Int64Chunked {
pub fn into_time(self) -> TimeChunked {
TimeChunked::new(self)
}
}

impl LogicalType for TimeChunked {
fn dtype(&self) -> &'static DataType {
&DataType::Time
}
}

0 comments on commit f70d219

Please sign in to comment.