pola-rs/polars · error

dtype not yet supported in checked div

Error message

dtype not yet supported in checked div

What it means

`Series::checked_div_num` divides a numeric Series by a scalar, but only a limited set of dtypes is implemented in its match; hitting the `_` arm panics with 'dtype not yet supported in checked div'. The result is then cast back to the original dtype, so only dtypes with a defined division path (e.g. Float64-backed arithmetic) are supported.

Solutions

  1. Cast the Series to Float64 before calling checked_div_num, then cast back if needed.
  2. Implement the division manually with `series.cast(DataType::Float64)?.divide(...)` or apply elementwise `floor_div`/`div` as appropriate.
  3. For integers, consider `checked_div`-style arithmetic operators or `Series::divide` which supports more dtypes.

Example fix

// before
let out = int_series.checked_div_num(2.0f64)?; // panics for this dtype

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

Strategy: type-guard

Validate before calling

match s.dtype() {
    DataType::Float64 | DataType::Float32 => { /* supported path */ }
    _ => { /* cast first */ }
}

Type guard

fn supports_checked_div_num(s: &Series) -> bool {
    matches!(s.dtype(), DataType::Float64 | DataType::Float32)
}

Prevention

When it happens

Trigger: Calling `series.checked_div_num(scalar)` on a Series whose dtype falls into the unimplemented match arm (e.g. certain integer/decimal/temporal dtypes); the visible caller is `test_checked_div`, but any user code invoking checked_div_num on such a dtype triggers it.

Common situations: Dividing integer or non-float columns by a float scalar and expecting automatic promotion; migrating code from `apply`-based division to checked_div_num without checking dtype coverage.

Related errors


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

Appendix: 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 fe841f959e)