pola-rs/polars · error

into_decimal( , ) not implemented for

Error message

into_decimal({precision:?}, {scale}) not implemented for {dt:?}

What it means

Series::into_decimal panics when the series' dtype cannot be converted to Decimal(precision, scale). Only Integers/Floats into decimal, or an existing decimal whose scale matches and whose precision fits within the target precision, are accepted.

Solutions

  1. Ensure the source dtype is Int/Float/Decimal before calling into_decimal
  2. Match the target scale exactly to the existing decimal's scale, and use precision >= current precision
  3. Use strict_cast(&DataType::Decimal(precision, scale)) which returns a PolarsResult instead of panicking

Example fix

// before
let s = series.into_decimal(Some(20), 10)?; // precision shrink -> panic
// after
let s = series.strict_cast(&DataType::Decimal(Some(38), 10))?;
Defensive patterns

Strategy: type-guard

Validate before calling

match s.dtype() { DataType::Int32 | DataType::Int64 | DataType::Float64 | DataType::Decimal(_, _) => {}, _ => return Err(...) }

Type guard

fn decidable_to_decimal(dt: &DataType) -> bool { matches!(dt, DataType::Int32 | DataType::Int64 | DataType::UInt32 | DataType::UInt64 | DataType::Float32 | DataType::Float64 | DataType::Decimal(_, _)) }

Try / catch

let s: PolarsResult<Series> = series.strict_cast(&DataType::Decimal(Some(prec), scale));

Prevention

When it happens

Trigger: Calling into_decimal(precision, scale) on a series whose dtype is not an integer/float/decimal, or on a decimal with scale != target scale, or with precision < current decimal precision.

Common situations: Shrinking decimal precision (e.g. Decimal(38,10) into precision 20) which would overflow; converting string or date series to decimal; mismatched scale expectations after reading parquet.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/1c7882c913d6a428. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-core/src/series/mod.rs:937

    pub fn strict_cast(&self, dtype: &DataType) -> PolarsResult<Series> {
        self.cast_with_options(dtype, CastOptions::Strict)
    }

    #[cfg(feature = "dtype-decimal")]
    pub fn into_decimal(self, precision: usize, scale: usize) -> PolarsResult<Series> {
        match self.dtype() {
            DataType::Int128 => Ok(self
                .i128()
                .unwrap()
                .clone()
                .into_decimal(precision, scale)?
                .into_series()),
            DataType::Decimal(cur_prec, cur_scale)
                if scale == *cur_scale && precision >= *cur_prec =>
            {
                Ok(self)
            },
            dt => panic!("into_decimal({precision:?}, {scale}) not implemented for {dt:?}"),
        }
    }

    #[cfg(feature = "dtype-time")]
    pub fn into_time(self) -> Series {
        match self.dtype() {
            DataType::Int64 => self.i64().unwrap().clone().into_time().into_series(),
            DataType::Time => self
                .time()
                .unwrap()
                .physical()
                .clone()
                .into_time()
                .into_series(),
            dt => panic!("date not implemented for {dt:?}"),
        }
    }

View on GitHub (pinned to fe841f959e)