{"record":{"id":"61b16b8b55e344c9","repo":"pola-rs/polars","slug":"cannot-coerce-datatypes","errorCode":null,"errorMessage":"cannot coerce datatypes","messagePattern":"cannot coerce datatypes","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/series/arithmetic/borrowed.rs","lineNumber":272,"sourceCode":"\n            let ca: Float64Chunked =\n                arity::binary_elementwise(lhs, rhs, |opt_l, opt_r| match (opt_l, opt_r) {\n                    (Some(l), Some(r)) => {\n                        if r.is_zero() {\n                            None\n                        } else {\n                            Some(l / r)\n                        }\n                    },\n                    _ => None,\n                });\n            Ok(ca.into_series())\n        }\n    }\n\n    impl NumOpsDispatchChecked for Series {\n        fn checked_div(&self, rhs: &Series) -> PolarsResult<Series> {\n            let (lhs, rhs) = coerce_lhs_rhs(self, rhs).expect(\"cannot coerce datatypes\");\n            lhs.as_ref().as_ref().checked_div(rhs.as_ref())\n        }\n\n        fn checked_div_num<T: ToPrimitive>(&self, rhs: T) -> PolarsResult<Series> {\n            use DataType::*;\n            let s = self.to_physical_repr();\n\n            let out = match s.dtype() {\n                #[cfg(feature = \"dtype-u8\")]\n                UInt8 => s\n                    .u8()\n                    .unwrap()\n                    .apply(|opt_v| opt_v.and_then(|v| v.checked_div(rhs.to_u8().unwrap())))\n                    .into_series(),\n                #[cfg(feature = \"dtype-i8\")]\n                Int8 => s\n                    .i8()\n                    .unwrap()","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-core/src/series/arithmetic/borrowed.rs#L254-L290","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nlet out = s1.checked_div(&s2).expect(\"cannot coerce datatypes\");\n\n// after\nlet l = s1.cast(&DataType::Float64)?;\nlet r = s2.cast(&DataType::Float64)?;\nlet out = l.checked_div(&r)?;","handlingStrategy":"validation","validationCode":"fn both_numeric(a: &Series, b: &Series) -> bool {\n    use DataType::*;\n    matches!(a.dtype(), Int8|Int16|Int32|Int64|UInt8|UInt16|UInt32|UInt64|Float32|Float64)\n        && matches!(b.dtype(), Int8|Int16|Int32|Int64|UInt8|UInt16|UInt32|UInt64|Float32|Float64)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["rust","polars","checked-div","dtype-coercion","panic","arithmetic"],"backgroundTag":"unsupported-operand-type","analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}