Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(rust, python): use try_binary_elementwise over try_binary_elementwise_values #11596

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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