Skip to content

Commit

Permalink
create Date/Datetime/Time/Duration -Chunked from iterators (#2568)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Feb 7, 2022
1 parent 71a2491 commit 1ceffe9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
5 changes: 3 additions & 2 deletions polars/polars-core/src/chunked_array/temporal/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ impl DateChunked {
ca
}

pub fn new_from_naive_date(name: &str, v: &[NaiveDate]) -> Self {
let unit = v.iter().map(|v| naive_date_to_date(*v)).collect::<Vec<_>>();
/// Construct a new [`DateChunked`] from an iterator over [`NaiveDate`].
pub fn from_naive_date<I: IntoIterator<Item = NaiveDate>>(name: &str, v: I) -> Self {
let unit = v.into_iter().map(naive_date_to_date).collect::<Vec<_>>();
Int32Chunked::from_vec(name, unit).into()
}

Expand Down
16 changes: 12 additions & 4 deletions polars/polars-core/src/chunked_array/temporal/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,17 @@ impl DatetimeChunked {
ca
}

pub fn new_from_naive_datetime(name: &str, v: &[NaiveDateTime], tu: TimeUnit) -> Self {
/// Construct a new [`DatetimeChunked`] from an iterator over [`NaiveDateTime`].
pub fn from_naive_datetime<I: IntoIterator<Item = NaiveDateTime>>(
name: &str,
v: I,
tu: TimeUnit,
) -> Self {
let func = match tu {
TimeUnit::Nanoseconds => naive_datetime_to_datetime_ns,
TimeUnit::Milliseconds => naive_datetime_to_datetime_ms,
};
let vals = v.iter().map(func).collect_trusted::<Vec<_>>();
let vals = v.into_iter().map(|nd| func(&nd)).collect::<Vec<_>>();
Int64Chunked::from_vec(name, vals).into_datetime(tu, None)
}

Expand Down Expand Up @@ -221,8 +226,11 @@ mod test {
.collect();

// NOTE: the values are checked and correct.
let dt =
DatetimeChunked::new_from_naive_datetime("name", &datetimes, TimeUnit::Nanoseconds);
let dt = DatetimeChunked::from_naive_datetime(
"name",
datetimes.iter().copied(),
TimeUnit::Nanoseconds,
);
assert_eq!(
[
588470416000_000_000,
Expand Down
13 changes: 9 additions & 4 deletions polars/polars-core/src/chunked_array/temporal/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ impl DurationChunked {
self.2 = Some(Duration(tu))
}

pub fn new_from_duration(name: &str, v: &[ChronoDuration], tu: TimeUnit) -> Self {
/// Construct a new [`DurationChunked`] from an iterator over [`ChronoDuration`].
pub fn from_duration<I: IntoIterator<Item = ChronoDuration>>(
name: &str,
v: I,
tu: TimeUnit,
) -> Self {
let func = match tu {
TimeUnit::Nanoseconds => |v: &ChronoDuration| v.num_nanoseconds().unwrap(),
TimeUnit::Milliseconds => |v: &ChronoDuration| v.num_milliseconds(),
TimeUnit::Nanoseconds => |v: ChronoDuration| v.num_nanoseconds().unwrap(),
TimeUnit::Milliseconds => |v: ChronoDuration| v.num_milliseconds(),
};
let vals = v.iter().map(func).collect_trusted::<Vec<_>>();
let vals = v.into_iter().map(func).collect::<Vec<_>>();
Int64Chunked::from_vec(name, vals).into_duration(tu)
}

Expand Down
3 changes: 2 additions & 1 deletion polars/polars-core/src/chunked_array/temporal/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ impl TimeChunked {
self.apply_kernel_cast::<_, UInt32Type>(time_to_nanosecond)
}

pub fn new_from_naive_time(name: &str, v: &[NaiveTime]) -> Self {
/// Construct a new [`TimeChunked`] from an iterator over [`NaiveTime`].
pub fn from_naive_time(name: &str, v: &[NaiveTime]) -> Self {
let ca: NoNull<Int64Chunked> = v.iter().map(time_to_time64ns).collect_trusted();
let mut ca = ca.into_inner();
ca.rename(name);
Expand Down
24 changes: 12 additions & 12 deletions polars/polars-lazy/src/tests/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,19 @@ fn test_lazy_query_9() -> Result<()> {
fn test_lazy_query_10() {
use polars_core::export::chrono::Duration as ChronoDuration;
let date = NaiveDate::from_ymd(2021, 3, 5);
let x: Series = DatetimeChunked::new_from_naive_datetime(
let x: Series = DatetimeChunked::from_naive_datetime(
"x",
&*vec![
[
NaiveDateTime::new(date, NaiveTime::from_hms(12, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(13, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(14, 0, 0)),
],
TimeUnit::Nanoseconds,
)
.into();
let y: Series = DatetimeChunked::new_from_naive_datetime(
let y: Series = DatetimeChunked::from_naive_datetime(
"y",
&*vec![
[
NaiveDateTime::new(date, NaiveTime::from_hms(11, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(11, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(11, 0, 0)),
Expand All @@ -472,9 +472,9 @@ fn test_lazy_query_10() {
.select(&[(col("x") - col("y")).alias("z")])
.collect()
.unwrap();
let z: Series = DurationChunked::new_from_duration(
let z: Series = DurationChunked::from_duration(
"z",
&*vec![
[
ChronoDuration::hours(1),
ChronoDuration::hours(2),
ChronoDuration::hours(3),
Expand All @@ -483,19 +483,19 @@ fn test_lazy_query_10() {
)
.into();
assert!(out.column("z").unwrap().series_equal(&z));
let x: Series = DatetimeChunked::new_from_naive_datetime(
let x: Series = DatetimeChunked::from_naive_datetime(
"x",
&*vec![
[
NaiveDateTime::new(date, NaiveTime::from_hms(2, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(3, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(4, 0, 0)),
],
TimeUnit::Milliseconds,
)
.into();
let y: Series = DatetimeChunked::new_from_naive_datetime(
let y: Series = DatetimeChunked::from_naive_datetime(
"y",
&*vec![
[
NaiveDateTime::new(date, NaiveTime::from_hms(1, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(1, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(1, 0, 0)),
Expand Down Expand Up @@ -523,7 +523,7 @@ fn test_lazy_query_10() {
))]
fn test_lazy_query_7() {
let date = NaiveDate::from_ymd(2021, 3, 5);
let dates = vec![
let dates = [
NaiveDateTime::new(date, NaiveTime::from_hms(12, 0, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(12, 1, 0)),
NaiveDateTime::new(date, NaiveTime::from_hms(12, 2, 0)),
Expand All @@ -533,7 +533,7 @@ fn test_lazy_query_7() {
];
let data = vec![Some(1.), Some(2.), Some(3.), Some(4.), None, None];
let df = DataFrame::new(vec![
DatetimeChunked::new_from_naive_datetime("date", &*dates, TimeUnit::Nanoseconds).into(),
DatetimeChunked::from_naive_datetime("date", dates, TimeUnit::Nanoseconds).into(),
Series::new("data", data),
])
.unwrap();
Expand Down

0 comments on commit 1ceffe9

Please sign in to comment.