-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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: Expressify list.shift #11320
feat: Expressify list.shift #11320
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,47 @@ impl ListChunked { | |
unsafe { self.amortized_iter().for_each(f) } | ||
} | ||
|
||
/// Zip with a `ChunkedArray` then apply a binary function `F` elementwise. | ||
#[must_use] | ||
pub fn zip_and_apply_amortized<'a, T, I, F>(&'a self, ca: &'a ChunkedArray<T>, mut f: F) -> Self | ||
where | ||
T: PolarsDataType, | ||
&'a ChunkedArray<T>: IntoIterator<IntoIter = I>, | ||
I: TrustedLen<Item = Option<T::Physical<'a>>>, | ||
F: FnMut(Option<UnstableSeries<'a>>, Option<T::Physical<'a>>) -> Option<Series>, | ||
{ | ||
if self.is_empty() { | ||
return self.clone(); | ||
} | ||
let mut fast_explode = self.null_count() == 0; | ||
// SAFETY: unstable series never lives longer than the iterator. | ||
let mut out: ListChunked = unsafe { | ||
self.amortized_iter() | ||
.zip(ca) | ||
.map(|(opt_s, opt_v)| { | ||
let out = f(opt_s, opt_v); | ||
match out { | ||
Some(out) if out.is_empty() => { | ||
fast_explode = false; | ||
Some(out) | ||
}, | ||
None => { | ||
fast_explode = false; | ||
out | ||
}, | ||
_ => out, | ||
} | ||
}) | ||
.collect_trusted() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make this a bit more resilient to ambiguous dtypes. We can use In a later PR we can do the same with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pub fn zip_and_apply_amortized<'a, T, I, F>(&'a self, ca: &'a ChunkedArray<T>, mut f: F) -> Self This function will return
I tend do this for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that would be sufficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bad news: It seems that our implementation of So it requires There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let's leave it for now. Maybe we can take a look later. Want to have this in the release. :) |
||
}; | ||
|
||
out.rename(self.name()); | ||
if fast_explode { | ||
out.set_fast_explode(); | ||
} | ||
out | ||
} | ||
|
||
/// Apply a closure `F` elementwise. | ||
#[must_use] | ||
pub fn apply_amortized<'a, F>(&'a self, mut f: F) -> Self | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what name would be more appropriate, but I assume that having a safe version would be good.