pub trait ChunkSet<'a, A, B> {
// Required methods
fn scatter_single<I: IntoIterator<Item = IdxSize>>(
&'a self,
idx: I,
opt_value: Option<A>,
) -> PolarsResult<Self>
where Self: Sized;
fn scatter_with<I: IntoIterator<Item = IdxSize>, F>(
&'a self,
idx: I,
f: F,
) -> PolarsResult<Self>
where Self: Sized,
F: Fn(Option<A>) -> Option<B>;
fn set(
&'a self,
mask: &BooleanChunked,
opt_value: Option<A>,
) -> PolarsResult<Self>
where Self: Sized;
}
Expand description
Create a ChunkedArray
with new values by index or by boolean mask.
Note that these operations clone data. This is however the only way we can modify at mask or index level as the underlying Arrow arrays are immutable.
Required Methods§
Sourcefn scatter_single<I: IntoIterator<Item = IdxSize>>(
&'a self,
idx: I,
opt_value: Option<A>,
) -> PolarsResult<Self>where
Self: Sized,
fn scatter_single<I: IntoIterator<Item = IdxSize>>(
&'a self,
idx: I,
opt_value: Option<A>,
) -> PolarsResult<Self>where
Self: Sized,
Set the values at indexes idx
to some optional value Option<T>
.
§Example
let ca = UInt32Chunked::new("a".into(), &[1, 2, 3]);
let new = ca.scatter_single(vec![0, 1], Some(10)).unwrap();
assert_eq!(Vec::from(&new), &[Some(10), Some(10), Some(3)]);
Sourcefn scatter_with<I: IntoIterator<Item = IdxSize>, F>(
&'a self,
idx: I,
f: F,
) -> PolarsResult<Self>
fn scatter_with<I: IntoIterator<Item = IdxSize>, F>( &'a self, idx: I, f: F, ) -> PolarsResult<Self>
Set the values at indexes idx
by applying a closure to these values.
§Example
let ca = Int32Chunked::new("a".into(), &[1, 2, 3]);
let new = ca.scatter_with(vec![0, 1], |opt_v| opt_v.map(|v| v - 5)).unwrap();
assert_eq!(Vec::from(&new), &[Some(-4), Some(-3), Some(3)]);
Sourcefn set(
&'a self,
mask: &BooleanChunked,
opt_value: Option<A>,
) -> PolarsResult<Self>where
Self: Sized,
fn set(
&'a self,
mask: &BooleanChunked,
opt_value: Option<A>,
) -> PolarsResult<Self>where
Self: Sized,
Set the values where the mask evaluates to true
to some optional value Option<T>
.
§Example
let ca = Int32Chunked::new("a".into(), &[1, 2, 3]);
let mask = BooleanChunked::new("mask".into(), &[false, true, false]);
let new = ca.set(&mask, Some(5)).unwrap();
assert_eq!(Vec::from(&new), &[Some(1), Some(5), Some(3)]);