Struct polars_core::chunked_array::builder::PrimitiveChunkedBuilder
source · pub struct PrimitiveChunkedBuilder<T>where
T: PolarsNumericType,{ /* private fields */ }Implementations§
source§impl<T> PrimitiveChunkedBuilder<T>where
T: PolarsNumericType,
impl<T> PrimitiveChunkedBuilder<T>where
T: PolarsNumericType,
sourcepub fn new(name: &str, capacity: usize) -> Self
pub fn new(name: &str, capacity: usize) -> Self
Examples found in repository?
More examples
src/chunked_array/random.rs (line 214)
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
pub fn rand_normal(name: &str, length: usize, mean: f64, std_dev: f64) -> PolarsResult<Self> {
let normal = match Normal::new(mean, std_dev) {
Ok(dist) => dist,
Err(e) => return Err(PolarsError::ComputeError(format!("{e:?}").into())),
};
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, length);
let mut rng = rand::thread_rng();
for _ in 0..length {
let smpl = normal.sample(&mut rng);
let smpl = NumCast::from(smpl).unwrap();
builder.append_value(smpl)
}
Ok(builder.finish())
}
/// Create `ChunkedArray` with samples from a Standard Normal distribution.
pub fn rand_standard_normal(name: &str, length: usize) -> Self {
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, length);
let mut rng = rand::thread_rng();
for _ in 0..length {
let smpl: f64 = rng.sample(StandardNormal);
let smpl = NumCast::from(smpl).unwrap();
builder.append_value(smpl)
}
builder.finish()
}
/// Create `ChunkedArray` with samples from a Uniform distribution.
pub fn rand_uniform(name: &str, length: usize, low: f64, high: f64) -> Self {
let uniform = Uniform::new(low, high);
let mut builder = PrimitiveChunkedBuilder::<T>::new(name, length);
let mut rng = rand::thread_rng();
for _ in 0..length {
let smpl = uniform.sample(&mut rng);
let smpl = NumCast::from(smpl).unwrap();
builder.append_value(smpl)
}
builder.finish()
}src/frame/row.rs (line 517)
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
fn from(a: (&DataType, usize)) -> Self {
let (dt, len) = a;
use DataType::*;
match dt {
Boolean => AnyValueBuffer::Boolean(BooleanChunkedBuilder::new("", len)),
Int32 => AnyValueBuffer::Int32(PrimitiveChunkedBuilder::new("", len)),
Int64 => AnyValueBuffer::Int64(PrimitiveChunkedBuilder::new("", len)),
UInt32 => AnyValueBuffer::UInt32(PrimitiveChunkedBuilder::new("", len)),
UInt64 => AnyValueBuffer::UInt64(PrimitiveChunkedBuilder::new("", len)),
#[cfg(feature = "dtype-date")]
Date => AnyValueBuffer::Date(PrimitiveChunkedBuilder::new("", len)),
#[cfg(feature = "dtype-datetime")]
Datetime(tu, tz) => {
AnyValueBuffer::Datetime(PrimitiveChunkedBuilder::new("", len), *tu, tz.clone())
}
#[cfg(feature = "dtype-duration")]
Duration(tu) => AnyValueBuffer::Duration(PrimitiveChunkedBuilder::new("", len), *tu),
#[cfg(feature = "dtype-time")]
Time => AnyValueBuffer::Time(PrimitiveChunkedBuilder::new("", len)),
Float32 => AnyValueBuffer::Float32(PrimitiveChunkedBuilder::new("", len)),
Float64 => AnyValueBuffer::Float64(PrimitiveChunkedBuilder::new("", len)),
Utf8 => AnyValueBuffer::Utf8(Utf8ChunkedBuilder::new("", len, len * 5)),
// Struct and List can be recursive so use anyvalues for that
dt => AnyValueBuffer::All(dt.clone(), Vec::with_capacity(len)),
}
}src/chunked_array/ops/rolling_window.rs (line 96)
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())
}
}Trait Implementations§
source§impl<T> ChunkedBuilder<<T as PolarsNumericType>::Native, T> for PrimitiveChunkedBuilder<T>where
T: PolarsNumericType,
impl<T> ChunkedBuilder<<T as PolarsNumericType>::Native, T> for PrimitiveChunkedBuilder<T>where
T: PolarsNumericType,
source§fn append_value(&mut self, v: T::Native)
fn append_value(&mut self, v: T::Native)
Appends a value of type T into the builder
source§fn append_null(&mut self)
fn append_null(&mut self)
Appends a null slot into the builder