Trait polars_core::chunked_array::builder::ChunkedBuilder
source · pub trait ChunkedBuilder<N, T: PolarsDataType> {
fn append_value(&mut self, val: N);
fn append_null(&mut self);
fn finish(self) -> ChunkedArray<T>;
fn shrink_to_fit(&mut self);
fn append_option(&mut self, opt_val: Option<N>) { ... }
}Required Methods§
fn append_value(&mut self, val: N)
fn append_null(&mut self)
fn finish(self) -> ChunkedArray<T>
fn shrink_to_fit(&mut self)
Provided Methods§
sourcefn append_option(&mut self, opt_val: Option<N>)
fn append_option(&mut self, opt_val: Option<N>)
Examples found in repository?
src/chunked_array/builder/mod.rs (line 85)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
fn from_iter_options(
name: &str,
it: impl Iterator<Item = Option<T::Native>>,
) -> ChunkedArray<T> {
let mut builder = PrimitiveChunkedBuilder::new(name, get_iter_capacity(&it));
it.for_each(|opt| builder.append_option(opt));
builder.finish()
}
/// Create a new ChunkedArray from an iterator.
fn from_iter_values(name: &str, it: impl Iterator<Item = T::Native>) -> ChunkedArray<T> {
let ca: NoNull<ChunkedArray<_>> = it.collect();
let mut ca = ca.into_inner();
ca.rename(name);
ca
}
}
impl NewChunkedArray<BooleanType, bool> for BooleanChunked {
fn from_slice(name: &str, v: &[bool]) -> Self {
Self::from_iter_values(name, v.iter().copied())
}
fn from_slice_options(name: &str, opt_v: &[Option<bool>]) -> Self {
Self::from_iter_options(name, opt_v.iter().copied())
}
fn from_iter_options(
name: &str,
it: impl Iterator<Item = Option<bool>>,
) -> ChunkedArray<BooleanType> {
let mut builder = BooleanChunkedBuilder::new(name, get_iter_capacity(&it));
it.for_each(|opt| builder.append_option(opt));
builder.finish()
}More examples
src/chunked_array/ops/rolling_window.rs (line 144)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
fn rolling_apply(
&self,
f: &dyn Fn(&Series) -> Series,
options: RollingOptionsFixedWindow,
) -> PolarsResult<Series> {
check_input(options.window_size, options.min_periods)?;
let ca = self.rechunk();
if options.weights.is_some()
&& !matches!(self.dtype(), DataType::Float64 | DataType::Float32)
{
let s = self.cast(&DataType::Float64)?;
return s.rolling_apply(f, options);
}
if options.window_size >= self.len() {
return Ok(Self::full_null(self.name(), self.len()).into_series());
}
let len = self.len();
let arr = ca.downcast_iter().next().unwrap();
let mut series_container =
ChunkedArray::<T>::from_slice("", &[T::Native::zero()]).into_series();
let array_ptr = series_container.array_ref(0);
let ptr = array_ptr.as_ref() as *const dyn Array as *mut dyn Array
as *mut PrimitiveArray<T::Native>;
let mut builder = PrimitiveChunkedBuilder::<T>::new(self.name(), self.len());
if let Some(weights) = options.weights {
let weights_series = Float64Chunked::new("weights", &weights).into_series();
let weights_series = weights_series.cast(self.dtype()).unwrap();
for idx in 0..len {
let (start, size) = window_edges(idx, len, options.window_size, options.center);
if size < options.min_periods {
builder.append_null();
} else {
// safety:
// we are in bounds
let arr_window = unsafe { arr.slice_unchecked(start, size) };
// Safety.
// ptr is not dropped as we are in scope
// We are also the only owner of the contents of the Arc
// we do this to reduce heap allocs.
unsafe {
*ptr = arr_window;
}
// ensure the length is correct
series_container._get_inner_mut().compute_len();
let s = if size == options.window_size {
f(&series_container.multiply(&weights_series).unwrap())
} else {
let weights_cutoff: Series = match self.dtype() {
DataType::Float64 => weights_series
.f64()
.unwrap()
.into_iter()
.take(series_container.len())
.collect(),
_ => weights_series // Float32 case
.f32()
.unwrap()
.into_iter()
.take(series_container.len())
.collect(),
};
f(&series_container.multiply(&weights_cutoff).unwrap())
};
let out = self.unpack_series_matching_type(&s)?;
builder.append_option(out.get(0));
}
}
Ok(builder.finish().into_series())
} else {
for idx in 0..len {
let (start, size) = window_edges(idx, len, options.window_size, options.center);
if size < options.min_periods {
builder.append_null();
} else {
// safety:
// we are in bounds
let arr_window = unsafe { arr.slice_unchecked(start, size) };
// Safety.
// ptr is not dropped as we are in scope
// We are also the only owner of the contents of the Arc
// we do this to reduce heap allocs.
unsafe {
*ptr = arr_window;
}
// ensure the length is correct
series_container._get_inner_mut().compute_len();
let s = f(&series_container);
let out = self.unpack_series_matching_type(&s)?;
builder.append_option(out.get(0));
}
}
Ok(builder.finish().into_series())
}
}