pola-rs/polars · error
cannot coerce datatypes
Error message
cannot coerce datatypes
What it means
Series::checked_div first coerces lhs and rhs to a common dtype via coerce_lhs_rhs (which handles time units and numeric supertypes). For dtype pairs with no common supertype - e.g. Utf8 vs Int64 - coercion returns an error, and the .expect("cannot coerce datatypes") turns it into a panic.
Solutions
- Cast both operands to a common numeric dtype before dividing: lhs.cast(&DataType::Float64)?
- Fix ingestion so numeric columns are not inferred as strings (cast during scan, correct dtypes in schema)
- Validate dtypes are numeric before performing arithmetic and return a proper error otherwise
Example fix
// before
let out = s1.checked_div(&s2).expect("cannot coerce datatypes");
// after
let l = s1.cast(&DataType::Float64)?;
let r = s2.cast(&DataType::Float64)?;
let out = l.checked_div(&r)?; Defensive patterns
Strategy: validation
Validate before calling
fn both_numeric(a: &Series, b: &Series) -> bool {
use DataType::*;
matches!(a.dtype(), Int8|Int16|Int32|Int64|UInt8|UInt16|UInt32|UInt64|Float32|Float64)
&& matches!(b.dtype(), Int8|Int16|Int32|Int64|UInt8|UInt16|UInt32|UInt64|Float32|Float64)
} Prevention
- Cast both operands to a common numeric dtype before checked_div
- Validate column dtypes at plan/ingest time so strings never reach arithmetic
- Watch for numeric columns inferred as Utf8 from CSV/JSON and cast during the scan
When it happens
Trigger: Calling s1.checked_div(&s2) (or / with overflow checks, e.g. eager division on mismatched columns) where one side is a string/categorical/boolean series and the other numeric, or two otherwise incompatible nested dtypes.
Common situations: Eager arithmetic on columns whose dtypes were inferred differently (ID column parsed as String in one frame, Int64 in another); applying division to a column that was accidentally read as Utf8 from CSV/JSON.
Related errors
- dtype not yet supported in checked div
- activate dtype
- adding decimals with different precisions/scales is not…
- adding durations with different units is not supported here
- could not cast
AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19).
Data as JSON: /api/errors/61b16b8b55e344c9.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-core/src/series/arithmetic/borrowed.rs:272
let ca: Float64Chunked =
arity::binary_elementwise(lhs, rhs, |opt_l, opt_r| match (opt_l, opt_r) {
(Some(l), Some(r)) => {
if r.is_zero() {
None
} else {
Some(l / r)
}
},
_ => None,
});
Ok(ca.into_series())
}
}
impl NumOpsDispatchChecked for Series {
fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {
let (lhs, rhs) = coerce_lhs_rhs(self, rhs).expect("cannot coerce datatypes");
lhs.as_ref().as_ref().checked_div(rhs.as_ref())
}
fn checked_div_num<T: ToPrimitive>(&self, rhs: T) -> PolarsResult<Series> {
use DataType::*;
let s = self.to_physical_repr();
let out = match s.dtype() {
#[cfg(feature = "dtype-u8")]
UInt8 => s
.u8()
.unwrap()
.apply(|opt_v| opt_v.and_then(|v| v.checked_div(rhs.to_u8().unwrap())))
.into_series(),
#[cfg(feature = "dtype-i8")]
Int8 => s
.i8()
.unwrap()View on GitHub (pinned to 9b5d73fd00)