Struct polars_core::chunked_array::builder::Utf8ChunkedBuilder
source · pub struct Utf8ChunkedBuilder {
pub capacity: usize,
/* private fields */
}Fields§
§capacity: usizeImplementations§
source§impl Utf8ChunkedBuilder
impl Utf8ChunkedBuilder
sourcepub fn new(name: &str, capacity: usize, bytes_capacity: usize) -> Self
pub fn new(name: &str, capacity: usize, bytes_capacity: usize) -> Self
Create a new UtfChunkedBuilder
Arguments
capacity- Number of string elements in the final array.bytes_capacity- Number of bytes needed to store the string values.
Examples found in repository?
More examples
src/chunked_array/builder/mod.rs (line 152)
150 151 152 153 154 155 156 157 158 159 160 161 162 163
fn from_iter_options(name: &str, it: impl Iterator<Item = Option<S>>) -> Self {
let cap = get_iter_capacity(&it);
let mut builder = Utf8ChunkedBuilder::new(name, cap, cap * 5);
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 = S>) -> Self {
let cap = get_iter_capacity(&it);
let mut builder = Utf8ChunkedBuilder::new(name, cap, cap * 5);
it.for_each(|v| builder.append_value(v));
builder.finish()
}src/chunked_array/logical/categorical/mod.rs (line 161)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn cast(&self, dtype: &DataType) -> PolarsResult<Series> {
match dtype {
DataType::Utf8 => {
let mapping = &**self.get_rev_map();
let mut builder =
Utf8ChunkedBuilder::new(self.logical.name(), self.len(), self.len() * 5);
let f = |idx: u32| mapping.get(idx);
if !self.logical.has_validity() {
self.logical
.into_no_null_iter()
.for_each(|idx| builder.append_value(f(idx)));
} else {
self.logical.into_iter().for_each(|opt_idx| {
builder.append_option(opt_idx.map(f));
});
}
let ca = builder.finish();
Ok(ca.into_series())
}
DataType::UInt32 => {
let ca =
UInt32Chunked::from_chunks(self.logical.name(), self.logical.chunks.clone());
Ok(ca.into_series())
}
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => Ok(self.clone().into_series()),
_ => self.logical.cast(dtype),
}
}src/chunked_array/ops/set.rs (line 215)
205 206 207 208 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 248 249 250 251 252 253 254 255 256 257
fn set_at_idx<I: IntoIterator<Item = IdxSize>>(
&'a self,
idx: I,
opt_value: Option<&'a str>,
) -> PolarsResult<Self>
where
Self: Sized,
{
let idx_iter = idx.into_iter();
let mut ca_iter = self.into_iter().enumerate();
let mut builder = Utf8ChunkedBuilder::new(self.name(), self.len(), self.get_values_size());
for current_idx in idx_iter {
if current_idx as usize > self.len() {
return Err(PolarsError::ComputeError(
format!(
"index: {} outside of ChunkedArray with length: {}",
current_idx,
self.len()
)
.into(),
));
}
for (cnt_idx, opt_val_self) in &mut ca_iter {
if cnt_idx == current_idx as usize {
builder.append_option(opt_value);
break;
} else {
builder.append_option(opt_val_self);
}
}
}
// the last idx is probably not the last value so we finish the iterator
for (_, opt_val_self) in ca_iter {
builder.append_option(opt_val_self);
}
let ca = builder.finish();
Ok(ca)
}
fn set_at_idx_with<I: IntoIterator<Item = IdxSize>, F>(
&'a self,
idx: I,
f: F,
) -> PolarsResult<Self>
where
Self: Sized,
F: Fn(Option<&'a str>) -> Option<String>,
{
let mut builder = Utf8ChunkedBuilder::new(self.name(), self.len(), self.get_values_size());
impl_set_at_idx_with!(self, builder, idx, f)
}src/chunked_array/ops/explode.rs (line 249)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
fn explode_by_offsets(&self, offsets: &[i64]) -> Series {
debug_assert_eq!(self.chunks.len(), 1);
let arr = self.downcast_iter().next().unwrap();
let cap = ((arr.len() as f32) * 1.5) as usize;
let bytes_size = self.get_values_size();
let mut builder = Utf8ChunkedBuilder::new(self.name(), cap, bytes_size);
let mut start = offsets[0] as usize;
let mut last = start;
for &o in &offsets[1..] {
let o = o as usize;
if o == last {
if start != last {
let vals = arr.slice(start, last - start);
if vals.null_count() == 0 {
builder
.builder
.extend_trusted_len_values(vals.values_iter())
} else {
builder.builder.extend_trusted_len(vals.into_iter());
}
}
builder.append_null();
start = o;
}
last = o;
}
let vals = arr.slice(start, last - start);
if vals.null_count() == 0 {
builder
.builder
.extend_trusted_len_values(vals.values_iter())
} else {
builder.builder.extend_trusted_len(vals.into_iter());
}
builder.finish().into()
}Additional examples can be found in:
sourcepub fn append_value<S: AsRef<str>>(&mut self, v: S)
pub fn append_value<S: AsRef<str>>(&mut self, v: S)
Appends a value of type T into the builder
Examples found in repository?
More examples
src/chunked_array/logical/categorical/mod.rs (line 168)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn cast(&self, dtype: &DataType) -> PolarsResult<Series> {
match dtype {
DataType::Utf8 => {
let mapping = &**self.get_rev_map();
let mut builder =
Utf8ChunkedBuilder::new(self.logical.name(), self.len(), self.len() * 5);
let f = |idx: u32| mapping.get(idx);
if !self.logical.has_validity() {
self.logical
.into_no_null_iter()
.for_each(|idx| builder.append_value(f(idx)));
} else {
self.logical.into_iter().for_each(|opt_idx| {
builder.append_option(opt_idx.map(f));
});
}
let ca = builder.finish();
Ok(ca.into_series())
}
DataType::UInt32 => {
let ca =
UInt32Chunked::from_chunks(self.logical.name(), self.logical.chunks.clone());
Ok(ca.into_series())
}
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => Ok(self.clone().into_series()),
_ => self.logical.cast(dtype),
}
}src/functions.rs (line 206)
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
pub fn concat_str(s: &[Series], delimiter: &str) -> PolarsResult<Utf8Chunked> {
if s.is_empty() {
return Err(PolarsError::NoData(
"expected multiple series in concat_str function".into(),
));
}
if s.iter().any(|s| s.is_empty()) {
return Ok(Utf8Chunked::full_null(s[0].name(), 0));
}
let len = s.iter().map(|s| s.len()).max().unwrap();
let cas = s
.iter()
.map(|s| {
let s = s.cast(&DataType::Utf8)?;
let mut ca = s.utf8()?.clone();
// broadcast
if ca.len() == 1 && len > 1 {
ca = ca.new_from_index(0, len)
}
Ok(ca)
})
.collect::<PolarsResult<Vec<_>>>()?;
if !s.iter().all(|s| s.len() == 1 || s.len() == len) {
return Err(PolarsError::ComputeError(
"All series in concat_str function should have equal length or unit length".into(),
));
}
let mut iters = cas
.iter()
.map(|ca| match ca.len() {
1 => IterBroadCast::Value(ca.get(0)),
_ => IterBroadCast::Column(ca.into_iter()),
})
.collect::<Vec<_>>();
let bytes_cap = cas.iter().map(|ca| ca.get_values_size()).sum();
let mut builder = Utf8ChunkedBuilder::new(s[0].name(), len, bytes_cap);
// use a string buffer, to amortize alloc
let mut buf = String::with_capacity(128);
for _ in 0..len {
let mut has_null = false;
iters.iter_mut().enumerate().for_each(|(i, it)| {
if i > 0 {
buf.push_str(delimiter);
}
match it.next() {
Some(Some(s)) => buf.push_str(s),
Some(None) => has_null = true,
None => {
// should not happen as the out loop counts to length
unreachable!()
}
}
});
if has_null {
builder.append_null();
} else {
builder.append_value(&buf)
}
buf.truncate(0)
}
Ok(builder.finish())
}src/frame/row.rs (line 456)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
pub fn add(&mut self, val: AnyValue<'a>) -> Option<()> {
use AnyValueBuffer::*;
match (self, val) {
(Boolean(builder), AnyValue::Null) => builder.append_null(),
(Boolean(builder), AnyValue::Boolean(v)) => builder.append_value(v),
(Boolean(builder), val) => {
let v = val.extract::<u8>()?;
builder.append_value(v == 1)
}
(Int32(builder), AnyValue::Null) => builder.append_null(),
(Int32(builder), val) => builder.append_value(val.extract()?),
(Int64(builder), AnyValue::Null) => builder.append_null(),
(Int64(builder), val) => builder.append_value(val.extract()?),
(UInt32(builder), AnyValue::Null) => builder.append_null(),
(UInt32(builder), val) => builder.append_value(val.extract()?),
(UInt64(builder), AnyValue::Null) => builder.append_null(),
(UInt64(builder), val) => builder.append_value(val.extract()?),
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Date(v)) => builder.append_value(v),
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, _, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, tu_l, _), AnyValue::Datetime(v, tu_r, _)) => {
// we convert right tu to left tu
// so we swap.
let v = convert_time_units(v, tu_r, *tu_l);
builder.append_value(v)
}
#[cfg(feature = "dtype-duration")]
(Duration(builder, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-duration")]
(Duration(builder, tu_l), AnyValue::Duration(v, tu_r)) => {
let v = convert_time_units(v, tu_r, *tu_l);
builder.append_value(v)
}
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Time(v)) => builder.append_value(v),
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Null) => builder.append_null(),
(Float32(builder), AnyValue::Null) => builder.append_null(),
(Float64(builder), AnyValue::Null) => builder.append_null(),
(Float32(builder), val) => builder.append_value(val.extract()?),
(Float64(builder), val) => builder.append_value(val.extract()?),
(Utf8(builder), AnyValue::Utf8(v)) => builder.append_value(v),
(Utf8(builder), AnyValue::Utf8Owned(v)) => builder.append_value(v),
(Utf8(builder), AnyValue::Null) => builder.append_null(),
// Struct and List can be recursive so use anyvalues for that
(All(_, vals), v) => vals.push(v),
// dynamic types
(Utf8(builder), av) => match av {
AnyValue::Int64(v) => builder.append_value(&format!("{v}")),
AnyValue::Float64(v) => builder.append_value(&format!("{v}")),
AnyValue::Boolean(true) => builder.append_value("true"),
AnyValue::Boolean(false) => builder.append_value("false"),
_ => return None,
},
_ => return None,
};
Some(())
}sourcepub fn append_null(&mut self)
pub fn append_null(&mut self)
Appends a null slot into the builder
Examples found in repository?
More examples
src/chunked_array/ops/explode.rs (line 266)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
fn explode_by_offsets(&self, offsets: &[i64]) -> Series {
debug_assert_eq!(self.chunks.len(), 1);
let arr = self.downcast_iter().next().unwrap();
let cap = ((arr.len() as f32) * 1.5) as usize;
let bytes_size = self.get_values_size();
let mut builder = Utf8ChunkedBuilder::new(self.name(), cap, bytes_size);
let mut start = offsets[0] as usize;
let mut last = start;
for &o in &offsets[1..] {
let o = o as usize;
if o == last {
if start != last {
let vals = arr.slice(start, last - start);
if vals.null_count() == 0 {
builder
.builder
.extend_trusted_len_values(vals.values_iter())
} else {
builder.builder.extend_trusted_len(vals.into_iter());
}
}
builder.append_null();
start = o;
}
last = o;
}
let vals = arr.slice(start, last - start);
if vals.null_count() == 0 {
builder
.builder
.extend_trusted_len_values(vals.values_iter())
} else {
builder.builder.extend_trusted_len(vals.into_iter());
}
builder.finish().into()
}src/functions.rs (line 204)
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
pub fn concat_str(s: &[Series], delimiter: &str) -> PolarsResult<Utf8Chunked> {
if s.is_empty() {
return Err(PolarsError::NoData(
"expected multiple series in concat_str function".into(),
));
}
if s.iter().any(|s| s.is_empty()) {
return Ok(Utf8Chunked::full_null(s[0].name(), 0));
}
let len = s.iter().map(|s| s.len()).max().unwrap();
let cas = s
.iter()
.map(|s| {
let s = s.cast(&DataType::Utf8)?;
let mut ca = s.utf8()?.clone();
// broadcast
if ca.len() == 1 && len > 1 {
ca = ca.new_from_index(0, len)
}
Ok(ca)
})
.collect::<PolarsResult<Vec<_>>>()?;
if !s.iter().all(|s| s.len() == 1 || s.len() == len) {
return Err(PolarsError::ComputeError(
"All series in concat_str function should have equal length or unit length".into(),
));
}
let mut iters = cas
.iter()
.map(|ca| match ca.len() {
1 => IterBroadCast::Value(ca.get(0)),
_ => IterBroadCast::Column(ca.into_iter()),
})
.collect::<Vec<_>>();
let bytes_cap = cas.iter().map(|ca| ca.get_values_size()).sum();
let mut builder = Utf8ChunkedBuilder::new(s[0].name(), len, bytes_cap);
// use a string buffer, to amortize alloc
let mut buf = String::with_capacity(128);
for _ in 0..len {
let mut has_null = false;
iters.iter_mut().enumerate().for_each(|(i, it)| {
if i > 0 {
buf.push_str(delimiter);
}
match it.next() {
Some(Some(s)) => buf.push_str(s),
Some(None) => has_null = true,
None => {
// should not happen as the out loop counts to length
unreachable!()
}
}
});
if has_null {
builder.append_null();
} else {
builder.append_value(&buf)
}
buf.truncate(0)
}
Ok(builder.finish())
}src/frame/row.rs (line 458)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
pub fn add(&mut self, val: AnyValue<'a>) -> Option<()> {
use AnyValueBuffer::*;
match (self, val) {
(Boolean(builder), AnyValue::Null) => builder.append_null(),
(Boolean(builder), AnyValue::Boolean(v)) => builder.append_value(v),
(Boolean(builder), val) => {
let v = val.extract::<u8>()?;
builder.append_value(v == 1)
}
(Int32(builder), AnyValue::Null) => builder.append_null(),
(Int32(builder), val) => builder.append_value(val.extract()?),
(Int64(builder), AnyValue::Null) => builder.append_null(),
(Int64(builder), val) => builder.append_value(val.extract()?),
(UInt32(builder), AnyValue::Null) => builder.append_null(),
(UInt32(builder), val) => builder.append_value(val.extract()?),
(UInt64(builder), AnyValue::Null) => builder.append_null(),
(UInt64(builder), val) => builder.append_value(val.extract()?),
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-date")]
(Date(builder), AnyValue::Date(v)) => builder.append_value(v),
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, _, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-datetime")]
(Datetime(builder, tu_l, _), AnyValue::Datetime(v, tu_r, _)) => {
// we convert right tu to left tu
// so we swap.
let v = convert_time_units(v, tu_r, *tu_l);
builder.append_value(v)
}
#[cfg(feature = "dtype-duration")]
(Duration(builder, _), AnyValue::Null) => builder.append_null(),
#[cfg(feature = "dtype-duration")]
(Duration(builder, tu_l), AnyValue::Duration(v, tu_r)) => {
let v = convert_time_units(v, tu_r, *tu_l);
builder.append_value(v)
}
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Time(v)) => builder.append_value(v),
#[cfg(feature = "dtype-time")]
(Time(builder), AnyValue::Null) => builder.append_null(),
(Float32(builder), AnyValue::Null) => builder.append_null(),
(Float64(builder), AnyValue::Null) => builder.append_null(),
(Float32(builder), val) => builder.append_value(val.extract()?),
(Float64(builder), val) => builder.append_value(val.extract()?),
(Utf8(builder), AnyValue::Utf8(v)) => builder.append_value(v),
(Utf8(builder), AnyValue::Utf8Owned(v)) => builder.append_value(v),
(Utf8(builder), AnyValue::Null) => builder.append_null(),
// Struct and List can be recursive so use anyvalues for that
(All(_, vals), v) => vals.push(v),
// dynamic types
(Utf8(builder), av) => match av {
AnyValue::Int64(v) => builder.append_value(&format!("{v}")),
AnyValue::Float64(v) => builder.append_value(&format!("{v}")),
AnyValue::Boolean(true) => builder.append_value("true"),
AnyValue::Boolean(false) => builder.append_value("false"),
_ => return None,
},
_ => return None,
};
Some(())
}sourcepub fn append_option<S: AsRef<str>>(&mut self, opt: Option<S>)
pub fn append_option<S: AsRef<str>>(&mut self, opt: Option<S>)
Examples found in repository?
More examples
src/chunked_array/logical/categorical/mod.rs (line 171)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn cast(&self, dtype: &DataType) -> PolarsResult<Series> {
match dtype {
DataType::Utf8 => {
let mapping = &**self.get_rev_map();
let mut builder =
Utf8ChunkedBuilder::new(self.logical.name(), self.len(), self.len() * 5);
let f = |idx: u32| mapping.get(idx);
if !self.logical.has_validity() {
self.logical
.into_no_null_iter()
.for_each(|idx| builder.append_value(f(idx)));
} else {
self.logical.into_iter().for_each(|opt_idx| {
builder.append_option(opt_idx.map(f));
});
}
let ca = builder.finish();
Ok(ca.into_series())
}
DataType::UInt32 => {
let ca =
UInt32Chunked::from_chunks(self.logical.name(), self.logical.chunks.clone());
Ok(ca.into_series())
}
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => Ok(self.clone().into_series()),
_ => self.logical.cast(dtype),
}
}src/chunked_array/ops/set.rs (line 230)
205 206 207 208 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
fn set_at_idx<I: IntoIterator<Item = IdxSize>>(
&'a self,
idx: I,
opt_value: Option<&'a str>,
) -> PolarsResult<Self>
where
Self: Sized,
{
let idx_iter = idx.into_iter();
let mut ca_iter = self.into_iter().enumerate();
let mut builder = Utf8ChunkedBuilder::new(self.name(), self.len(), self.get_values_size());
for current_idx in idx_iter {
if current_idx as usize > self.len() {
return Err(PolarsError::ComputeError(
format!(
"index: {} outside of ChunkedArray with length: {}",
current_idx,
self.len()
)
.into(),
));
}
for (cnt_idx, opt_val_self) in &mut ca_iter {
if cnt_idx == current_idx as usize {
builder.append_option(opt_value);
break;
} else {
builder.append_option(opt_val_self);
}
}
}
// the last idx is probably not the last value so we finish the iterator
for (_, opt_val_self) in ca_iter {
builder.append_option(opt_val_self);
}
let ca = builder.finish();
Ok(ca)
}sourcepub fn finish(self) -> Utf8Chunked
pub fn finish(self) -> Utf8Chunked
Examples found in repository?
More examples
src/chunked_array/builder/mod.rs (line 154)
150 151 152 153 154 155 156 157 158 159 160 161 162 163
fn from_iter_options(name: &str, it: impl Iterator<Item = Option<S>>) -> Self {
let cap = get_iter_capacity(&it);
let mut builder = Utf8ChunkedBuilder::new(name, cap, cap * 5);
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 = S>) -> Self {
let cap = get_iter_capacity(&it);
let mut builder = Utf8ChunkedBuilder::new(name, cap, cap * 5);
it.for_each(|v| builder.append_value(v));
builder.finish()
}src/frame/row.rs (line 500)
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
pub fn into_series(self) -> Series {
use AnyValueBuffer::*;
match self {
Boolean(b) => b.finish().into_series(),
Int32(b) => b.finish().into_series(),
Int64(b) => b.finish().into_series(),
UInt32(b) => b.finish().into_series(),
UInt64(b) => b.finish().into_series(),
#[cfg(feature = "dtype-date")]
Date(b) => b.finish().into_date().into_series(),
#[cfg(feature = "dtype-datetime")]
Datetime(b, tu, tz) => b.finish().into_datetime(tu, tz).into_series(),
#[cfg(feature = "dtype-duration")]
Duration(b, tu) => b.finish().into_duration(tu).into_series(),
#[cfg(feature = "dtype-time")]
Time(b) => b.finish().into_time().into_series(),
Float32(b) => b.finish().into_series(),
Float64(b) => b.finish().into_series(),
Utf8(b) => b.finish().into_series(),
All(dtype, vals) => Series::from_any_values_and_dtype("", &vals, &dtype).unwrap(),
}
}src/chunked_array/logical/categorical/mod.rs (line 175)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
fn cast(&self, dtype: &DataType) -> PolarsResult<Series> {
match dtype {
DataType::Utf8 => {
let mapping = &**self.get_rev_map();
let mut builder =
Utf8ChunkedBuilder::new(self.logical.name(), self.len(), self.len() * 5);
let f = |idx: u32| mapping.get(idx);
if !self.logical.has_validity() {
self.logical
.into_no_null_iter()
.for_each(|idx| builder.append_value(f(idx)));
} else {
self.logical.into_iter().for_each(|opt_idx| {
builder.append_option(opt_idx.map(f));
});
}
let ca = builder.finish();
Ok(ca.into_series())
}
DataType::UInt32 => {
let ca =
UInt32Chunked::from_chunks(self.logical.name(), self.logical.chunks.clone());
Ok(ca.into_series())
}
#[cfg(feature = "dtype-categorical")]
DataType::Categorical(_) => Ok(self.clone().into_series()),
_ => self.logical.cast(dtype),
}
}src/chunked_array/ops/set.rs (line 242)
205 206 207 208 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
fn set_at_idx<I: IntoIterator<Item = IdxSize>>(
&'a self,
idx: I,
opt_value: Option<&'a str>,
) -> PolarsResult<Self>
where
Self: Sized,
{
let idx_iter = idx.into_iter();
let mut ca_iter = self.into_iter().enumerate();
let mut builder = Utf8ChunkedBuilder::new(self.name(), self.len(), self.get_values_size());
for current_idx in idx_iter {
if current_idx as usize > self.len() {
return Err(PolarsError::ComputeError(
format!(
"index: {} outside of ChunkedArray with length: {}",
current_idx,
self.len()
)
.into(),
));
}
for (cnt_idx, opt_val_self) in &mut ca_iter {
if cnt_idx == current_idx as usize {
builder.append_option(opt_value);
break;
} else {
builder.append_option(opt_val_self);
}
}
}
// the last idx is probably not the last value so we finish the iterator
for (_, opt_val_self) in ca_iter {
builder.append_option(opt_val_self);
}
let ca = builder.finish();
Ok(ca)
}Additional examples can be found in: