{"record":{"id":"6b10d56526d9af7e","repo":"pola-rs/polars","slug":"could-not-cast","errorCode":null,"errorMessage":"could not cast","messagePattern":"could not cast","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/series/arithmetic/borrowed.rs","lineNumber":835,"sourceCode":"impl<T> Rem<T> for Series\nwhere\n    T: Num + NumCast,\n{\n    type Output = Self;\n\n    fn rem(self, rhs: T) -> Self::Output {\n        (&self).rem(rhs)\n    }\n}\n\n/// We cannot override the left hand side behaviour. So we create a trait LhsNumOps.\n/// This allows for 1.add(&Series)\n///\nimpl<T: PolarsNumericType> ChunkedArray<T> {\n    /// Apply lhs - self\n    #[must_use]\n    pub fn lhs_sub<N: Num + NumCast>(&self, lhs: N) -> Self {\n        let lhs: T::Native = NumCast::from(lhs).expect(\"could not cast\");\n        ArithmeticChunked::wrapping_sub_scalar_lhs(lhs, self)\n    }\n\n    /// Apply lhs / self\n    #[must_use]\n    pub fn lhs_div<N: Num + NumCast>(&self, lhs: N) -> Self {\n        let lhs: T::Native = NumCast::from(lhs).expect(\"could not cast\");\n        ArithmeticChunked::legacy_div_scalar_lhs(lhs, self)\n    }\n\n    /// Apply lhs % self\n    #[must_use]\n    pub fn lhs_rem<N: Num + NumCast>(&self, lhs: N) -> Self {\n        let lhs: T::Native = NumCast::from(lhs).expect(\"could not cast\");\n        ArithmeticChunked::wrapping_mod_scalar_lhs(lhs, self)\n    }\n}\n","sourceCodeStart":817,"sourceCodeEnd":853,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-core/src/series/arithmetic/borrowed.rs#L817-L853","documentation":"ChunkedArray::lhs_sub lets a scalar be the left operand (lhs - array). The scalar is converted to the array's native type with NumCast::from; if the value cannot be represented in T::Native (e.g. 1.5f64 into i64, or 5e9 into i32), NumCast returns None and expect panics 'could not cast'.","triggerScenarios":"Calling lhs_sub-style scalar-on-the-left subtraction on an integer ChunkedArray with a non-representable scalar: 1.5_i64_fallback - ca_i64, or 4_000_000_000u64 - ca_i32.","commonSituations":"Generic code where N: Num + NumCast is instantiated with f64 or u64 while the chunked array is an integer type; constants written as floats (2.5 - s) against integer columns.","solutions":["Use a scalar that fits the array's native type (integer scalar for integer arrays)","Cast the chunked array to Float64 first, then apply the scalar operation","Use Series-level arithmetic (Series - Series), which coerces supertypes instead of hard-casting the scalar"],"exampleFix":"// before\nlet out = ca_i64.lhs_sub(1.5f64); // 1.5 not representable as i64\n\n// after\nlet out = ca_i64.cast(&DataType::Float64).unwrap().f64().unwrap().lhs_sub(1.5f64);","handlingStrategy":"validation","validationCode":"fn scalar_fits<N: ToPrimitive + NumCast>(v: N) -> bool {\n    // check against the array's native type before calling lhs_*\n    // e.g. for i64 arrays: v.to_i64().is_some() && v.to_f64().map_or(true, |f| f.fract() == 0.0)\n    v.to_i64().is_some()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Match scalar type to the array's native type (integer scalar for integer arrays)","Cast integer arrays to Float64 before applying float scalars","Prefer Series-level arithmetic, which supertypes operands instead of hard-casting"],"tags":["rust","polars","numcast","scalar-arithmetic","panic","type-mismatch"],"backgroundTag":"lossy-numeric-cast","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"}