Skip to content

Commit

Permalink
perf(rust, python): use try_binary_elementwise over try_binary_elemen…
Browse files Browse the repository at this point in the history
…twise_values (pola-rs#11596)
  • Loading branch information
MarcoGorelli authored Oct 9, 2023
1 parent 0ae1b6e commit 05d9eb2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 12 additions & 9 deletions crates/polars-ops/src/chunked_array/datetime/replace_time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use arrow::temporal_conversions::{
use chrono::NaiveDateTime;
use chrono_tz::{Tz, UTC};
use polars_arrow::kernels::convert_to_naive_local;
use polars_core::chunked_array::ops::arity::try_binary_elementwise_values;
use polars_core::chunked_array::ops::arity::try_binary_elementwise;
use polars_core::prelude::*;

fn parse_time_zone(s: &str) -> PolarsResult<Tz> {
Expand Down Expand Up @@ -51,14 +51,17 @@ pub fn replace_time_zone(
}),
_ => Ok(datetime.0.apply(|_| None)),
},
_ => {
try_binary_elementwise_values(datetime, ambiguous, |timestamp: i64, ambiguous: &str| {
let ndt = timestamp_to_datetime(timestamp);
Ok::<i64, PolarsError>(datetime_to_timestamp(convert_to_naive_local(
&from_tz, &to_tz, ndt, ambiguous,
)?))
})
},
_ => try_binary_elementwise(datetime, ambiguous, |timestamp_opt, ambiguous_opt| {
match (timestamp_opt, ambiguous_opt) {
(Some(timestamp), Some(ambiguous)) => {
let ndt = timestamp_to_datetime(timestamp);
Ok(Some(datetime_to_timestamp(convert_to_naive_local(
&from_tz, &to_tz, ndt, ambiguous,
)?)))
},
_ => Ok(None),
}
}),
};
let mut out = out?.into_datetime(datetime.time_unit(), time_zone.map(|x| x.to_string()));
if from_time_zone == "UTC" && ambiguous.len() == 1 && ambiguous.get(0).unwrap() == "raise" {
Expand Down
12 changes: 8 additions & 4 deletions crates/polars-plan/src/dsl/function_expr/temporal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(feature = "date_offset")]
use polars_arrow::time_zone::Tz;
#[cfg(feature = "date_offset")]
use polars_core::chunked_array::ops::arity::try_binary_elementwise_values;
use polars_core::chunked_array::ops::arity::try_binary_elementwise;
#[cfg(feature = "date_offset")]
use polars_time::prelude::*;

Expand Down Expand Up @@ -139,9 +139,13 @@ fn apply_offsets_to_datetime(
.try_apply(|v| offset_fn(&Duration::parse(offset), v, time_zone)),
_ => Ok(datetime.0.apply(|_| None)),
},
_ => try_binary_elementwise_values(datetime, offsets, |timestamp: i64, offset: &str| {
let offset = Duration::parse(offset);
offset_fn(&offset, timestamp, time_zone)
_ => try_binary_elementwise(datetime, offsets, |timestamp_opt, offset_opt| {
match (timestamp_opt, offset_opt) {
(Some(timestamp), Some(offset)) => {
offset_fn(&Duration::parse(offset), timestamp, time_zone).map(Some)
},
_ => Ok(None),
}
}),
}
}
Expand Down

0 comments on commit 05d9eb2

Please sign in to comment.