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

feat(rust!): Rename ChunkedArray.try_apply to try_apply_values #14947

Merged
merged 2 commits into from
Mar 21, 2024
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
12 changes: 6 additions & 6 deletions crates/polars-core/src/chunked_array/ops/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ where
ChunkedArray::from_chunk_iter(self.name(), chunks)
}

fn try_apply<F>(&'a self, f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(T::Native) -> PolarsResult<T::Native> + Copy,
{
Expand Down Expand Up @@ -315,7 +315,7 @@ impl<'a> ChunkApply<'a, bool> for BooleanChunked {
})
}

fn try_apply<F>(&self, f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&self, f: F) -> PolarsResult<Self>
where
F: Fn(bool) -> PolarsResult<bool> + Copy,
{
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<'a> ChunkApply<'a, &'a str> for StringChunked {
ChunkedArray::apply_values_generic(self, f)
}

fn try_apply<F>(&'a self, f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(&'a str) -> PolarsResult<Cow<'a, str>> + Copy,
{
Expand Down Expand Up @@ -474,7 +474,7 @@ impl<'a> ChunkApply<'a, &'a [u8]> for BinaryChunked {
self.apply_values_generic(f)
}

fn try_apply<F>(&'a self, f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(&'a [u8]) -> PolarsResult<Cow<'a, [u8]>> + Copy,
{
Expand Down Expand Up @@ -605,7 +605,7 @@ impl<'a> ChunkApply<'a, Series> for ListChunked {
ca
}

fn try_apply<F>(&'a self, f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(Series) -> PolarsResult<Series> + Copy,
{
Expand Down Expand Up @@ -686,7 +686,7 @@ where
ca
}

fn try_apply<F>(&'a self, _f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&'a self, _f: F) -> PolarsResult<Self>
where
F: Fn(&'a T) -> PolarsResult<T> + Copy,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub trait ChunkApply<'a, T> {
where
F: Fn(T) -> Self::FuncRet + Copy;

fn try_apply<F>(&'a self, f: F) -> PolarsResult<Self>
fn try_apply_values<F>(&'a self, f: F) -> PolarsResult<Self>
where
F: Fn(T) -> PolarsResult<Self::FuncRet> + Copy,
Self: Sized;
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-ops/src/chunked_array/binary/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait BinaryNameSpaceImpl: AsBinary {
fn hex_decode(&self, strict: bool) -> PolarsResult<BinaryChunked> {
let ca = self.as_binary();
if strict {
ca.try_apply(|s| {
ca.try_apply_values(|s| {
let bytes = hex::decode(s).map_err(|_| {
polars_err!(
ComputeError:
Expand Down Expand Up @@ -101,7 +101,7 @@ pub trait BinaryNameSpaceImpl: AsBinary {
fn base64_decode(&self, strict: bool) -> PolarsResult<BinaryChunked> {
let ca = self.as_binary();
if strict {
ca.try_apply(|s| {
ca.try_apply_values(|s| {
let bytes = general_purpose::STANDARD.decode(s).map_err(|_e| {
polars_err!(
ComputeError:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn impl_replace_time_zone_fast(
to_tz: &chrono_tz::Tz,
) -> PolarsResult<Int64Chunked> {
match ambiguous {
Some(ambiguous) => datetime.0.try_apply(|timestamp| {
Some(ambiguous) => datetime.0.try_apply_values(|timestamp| {
let ndt = timestamp_to_datetime(timestamp);
Ok(datetime_to_timestamp(
convert_to_naive_local(
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/temporal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn apply_offsets_to_datetime(
(_, 1) => match offsets.get(0) {
Some(offset) => datetime
.0
.try_apply(|v| offset_fn(&Duration::parse(offset), v, time_zone)),
.try_apply_values(|v| offset_fn(&Duration::parse(offset), v, time_zone)),
_ => Ok(datetime.0.apply(|_| None)),
},
_ => try_binary_elementwise(datetime, offsets, |timestamp_opt, offset_opt| {
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-time/src/month_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl PolarsMonthEnd for DatetimeChunked {
};
Ok(self
.0
.try_apply(|t| {
.try_apply_values(|t| {
roll_forward(
t,
time_zone,
Expand All @@ -70,7 +70,7 @@ impl PolarsMonthEnd for DateChunked {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok(self
.0
.try_apply(|t| {
.try_apply_values(|t| {
Ok((roll_forward(
MSECS_IN_DAY * t as i64,
None,
Expand Down
6 changes: 4 additions & 2 deletions crates/polars-time/src/month_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ impl PolarsMonthStart for DatetimeChunked {
};
Ok(self
.0
.try_apply(|t| roll_backward(t, tz, timestamp_to_datetime, datetime_to_timestamp))?
.try_apply_values(|t| {
roll_backward(t, tz, timestamp_to_datetime, datetime_to_timestamp)
})?
.into_datetime(self.time_unit(), self.time_zone().clone()))
}
}
Expand All @@ -92,7 +94,7 @@ impl PolarsMonthStart for DateChunked {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok(self
.0
.try_apply(|t| {
.try_apply_values(|t| {
Ok((roll_backward(
MSECS_IN_DAY * t as i64,
None,
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-time/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl PolarsRound for DatetimeChunked {
TimeUnit::Milliseconds => Window::round_ms,
};

let out = { self.try_apply(|t| func(&w, t, tz)) };
let out = { self.try_apply_values(|t| func(&w, t, tz)) };
out.map(|ok| ok.into_datetime(self.time_unit(), self.time_zone().clone()))
}
}
Expand All @@ -29,7 +29,7 @@ impl PolarsRound for DateChunked {
fn round(&self, every: Duration, offset: Duration, _tz: Option<&Tz>) -> PolarsResult<Self> {
let w = Window::new(every, every, offset);
Ok(self
.try_apply(|t| {
.try_apply_values(|t| {
const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok((w.round_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32)
})?
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-time/src/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl PolarsTruncate for DatetimeChunked {
if let Some(every) = every.get(0) {
let every = Duration::parse(every);
let w = Window::new(every, every, offset);
self.0.try_apply(|timestamp| func(&w, timestamp, tz))
self.0.try_apply_values(|timestamp| func(&w, timestamp, tz))
} else {
Ok(Int64Chunked::full_null(self.name(), self.len()))
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl PolarsTruncate for DateChunked {
if let Some(every) = every.get(0) {
let every = Duration::parse(every);
let w = Window::new(every, every, offset);
self.try_apply(|t| {
self.try_apply_values(|t| {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function looks expensive enough that it may be worth skipping null values for - with more consistent naming, this potential inefficiency jumps out a bit more

const MSECS_IN_DAY: i64 = MILLISECONDS * SECONDS_IN_DAY;
Ok((w.truncate_ms(MSECS_IN_DAY * t as i64, None)? / MSECS_IN_DAY) as i32)
})
Expand Down
Loading