polars_core/chunked_array/ops/
decimal.rsuse crate::chunked_array::cast::CastOptions;
use crate::prelude::*;
impl StringChunked {
pub fn to_decimal(&self, infer_length: usize) -> PolarsResult<Series> {
let mut scale = 0;
let mut iter = self.into_iter();
let mut valid_count = 0;
while let Some(Some(v)) = iter.next() {
let scale_value = arrow::compute::decimal::infer_scale(v.as_bytes());
scale = std::cmp::max(scale, scale_value);
valid_count += 1;
if valid_count == infer_length {
break;
}
}
self.cast_with_options(
&DataType::Decimal(None, Some(scale as usize)),
CastOptions::NonStrict,
)
}
}
#[cfg(test)]
mod test {
#[test]
fn test_inferred_length() {
use super::*;
let vals = [
"1.0",
"invalid",
"225.0",
"3.00045",
"-4.0",
"5.104",
"5.25251525353",
];
let s = StringChunked::from_slice(PlSmallStr::from_str("test"), &vals);
let s = s.to_decimal(6).unwrap();
assert_eq!(s.dtype(), &DataType::Decimal(None, Some(5)));
assert_eq!(s.len(), 7);
assert_eq!(s.get(0).unwrap(), AnyValue::Decimal(100000, 5));
assert_eq!(s.get(1).unwrap(), AnyValue::Null);
assert_eq!(s.get(3).unwrap(), AnyValue::Decimal(300045, 5));
assert_eq!(s.get(4).unwrap(), AnyValue::Decimal(-400000, 5));
assert_eq!(s.get(6).unwrap(), AnyValue::Decimal(525251, 5));
}
}