pola-rs/polars · error

dtype not yet supported in checked div

Error message

dtype not yet supported in checked div

What it means

checked_div_num divides a Series by a scalar and is implemented only for the fixed set of numeric dtypes (u8/u16/u32/u64/i8/i16/i32/i64/f32/f64). Any other dtype - Boolean, Utf8, Decimal, Categorical, List, Datetime in some feature sets - falls through to panic 'dtype not yet supported in checked div'.

Source

Thrown at crates/polars-core/src/series/arithmetic/borrowed.rs:345

                    .unwrap()
                    .apply(|opt_v| {
                        opt_v.and_then(|v| {
                            let res = rhs.to_f32().unwrap();
                            if res.is_zero() { None } else { Some(v / res) }
                        })
                    })
                    .into_series(),
                Float64 => s
                    .f64()
                    .unwrap()
                    .apply(|opt_v| {
                        opt_v.and_then(|v| {
                            let res = rhs.to_f64().unwrap();
                            if res.is_zero() { None } else { Some(v / res) }
                        })
                    })
                    .into_series(),
                _ => panic!("dtype not yet supported in checked div"),
            };
            out.cast(self.dtype())
        }
    }
}

pub fn coerce_lhs_rhs<'a>(
    lhs: &'a Series,
    rhs: &'a Series,
) -> PolarsResult<(Cow<'a, Series>, Cow<'a, Series>)> {
    if let Some(result) = coerce_time_units(lhs, rhs) {
        return Ok(result);
    }
    let (left_dtype, right_dtype) = (lhs.dtype(), rhs.dtype());
    let leaf_super_dtype = try_get_supertype(left_dtype.leaf_dtype(), right_dtype.leaf_dtype())?;

    let mut new_left_dtype = left_dtype.cast_leaf(leaf_super_dtype.clone());
    let mut new_right_dtype = right_dtype.cast_leaf(leaf_super_dtype);

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. Cast the series to a numeric dtype first: s.cast(&DataType::Float64)?.checked_div_num(2.0)
  2. Use full Series-to-Series checked_div with a broadcast series, which handles more dtypes
  3. Branch on the dtype and reject non-numeric columns with a clear error before dividing

Example fix

// before
let out = s.checked_div_num(2)?;

// after
let out = s.cast(&DataType::Float64)?.checked_div_num(2.0)?;
Defensive patterns

Strategy: validation

Validate before calling

fn is_scalar_divisible(s: &Series) -> bool {
    use DataType::*;
    matches!(s.dtype(), Int8|Int16|Int32|Int64|UInt8|UInt16|UInt32|UInt64|Float32|Float64)
}

Prevention

When it happens

Trigger: Calling s.checked_div_num(2) where s holds Boolean, String, Decimal, Categorical, or nested values; also division helpers that internally route scalar division through checked_div_num.

Common situations: Applying generic math (e.g. scaling a column) to a column that turned out non-numeric: booleans from a filter, decimal columns from financial data, categorical codes from enum columns.

Related errors


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/acd69bd528d08c36. Report an issue: GitHub.