Struct polars_core::prelude::CategoricalChunkedBuilder
source · pub struct CategoricalChunkedBuilder<'a> { /* private fields */ }Implementations§
source§impl CategoricalChunkedBuilder<'_>
impl CategoricalChunkedBuilder<'_>
sourcepub fn new(name: &str, capacity: usize) -> Self
pub fn new(name: &str, capacity: usize) -> Self
Examples found in repository?
src/chunked_array/cast.rs (line 111)
106 107 108 109 110 111 112 113 114 115 116 117 118
fn cast(&self, data_type: &DataType) -> PolarsResult<Series> {
match data_type {
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => {
let iter = self.into_iter();
let mut builder = CategoricalChunkedBuilder::new(self.name(), self.len());
builder.drain_iter(iter);
let ca = builder.finish();
Ok(ca.into_series())
}
_ => cast_impl(self.name(), &self.chunks, data_type),
}
}More examples
src/chunked_array/logical/categorical/from.rs (line 95)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
pub(crate) unsafe fn from_keys_and_values(
name: &str,
keys: &PrimitiveArray<u32>,
values: &Utf8Array<i64>,
) -> Self {
if using_string_cache() {
let mut builder = CategoricalChunkedBuilder::new(name, keys.len());
builder.global_map_from_local(keys, values.clone());
builder.finish()
} else {
CategoricalChunked::from_chunks_original(
name,
vec![Box::new(keys.clone())],
RevMapping::Local(values.clone()),
)
}
}source§impl<'a> CategoricalChunkedBuilder<'a>
impl<'a> CategoricalChunkedBuilder<'a>
pub fn append_value(&mut self, s: &'a str)
sourcepub fn append_null(&mut self)
pub fn append_null(&mut self)
Examples found in repository?
src/chunked_array/logical/categorical/builder.rs (line 248)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
fn build_local_map<I>(&mut self, i: I, store_hashes: bool) -> Vec<u64>
where
I: IntoIterator<Item = Option<&'a str>>,
{
let mut iter = i.into_iter();
if store_hashes {
self.hashes = Vec::with_capacity(iter.size_hint().0 / 10)
}
// It is important that we use the same hash builder as the global `StringCache` does.
self.local_mapping =
PlHashMap::with_capacity_and_hasher(HASHMAP_INIT_SIZE, StringCache::get_hash_builder());
for opt_s in &mut iter {
match opt_s {
Some(s) => self.push_impl(s, store_hashes),
None => self.append_null(),
}
}
if self.local_mapping.len() > u32::MAX as usize {
panic!("not more than {} categories supported", u32::MAX)
};
// drop the hashmap
std::mem::take(&mut self.local_mapping);
std::mem::take(&mut self.hashes)
}sourcepub fn drain_iter<I>(&mut self, i: I)where
I: IntoIterator<Item = Option<&'a str>>,
pub fn drain_iter<I>(&mut self, i: I)where
I: IntoIterator<Item = Option<&'a str>>,
Appends all the values in a single lock of the global string cache.
Examples found in repository?
src/chunked_array/cast.rs (line 112)
106 107 108 109 110 111 112 113 114 115 116 117 118
fn cast(&self, data_type: &DataType) -> PolarsResult<Series> {
match data_type {
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => {
let iter = self.into_iter();
let mut builder = CategoricalChunkedBuilder::new(self.name(), self.len());
builder.drain_iter(iter);
let ca = builder.finish();
Ok(ca.into_series())
}
_ => cast_impl(self.name(), &self.chunks, data_type),
}
}sourcepub fn finish(self) -> CategoricalChunked
pub fn finish(self) -> CategoricalChunked
Examples found in repository?
src/chunked_array/cast.rs (line 113)
106 107 108 109 110 111 112 113 114 115 116 117 118
fn cast(&self, data_type: &DataType) -> PolarsResult<Series> {
match data_type {
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => {
let iter = self.into_iter();
let mut builder = CategoricalChunkedBuilder::new(self.name(), self.len());
builder.drain_iter(iter);
let ca = builder.finish();
Ok(ca.into_series())
}
_ => cast_impl(self.name(), &self.chunks, data_type),
}
}More examples
src/chunked_array/logical/categorical/from.rs (line 97)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
pub(crate) unsafe fn from_keys_and_values(
name: &str,
keys: &PrimitiveArray<u32>,
values: &Utf8Array<i64>,
) -> Self {
if using_string_cache() {
let mut builder = CategoricalChunkedBuilder::new(name, keys.len());
builder.global_map_from_local(keys, values.clone());
builder.finish()
} else {
CategoricalChunked::from_chunks_original(
name,
vec![Box::new(keys.clone())],
RevMapping::Local(values.clone()),
)
}
}