pola-rs/polars · error
could not cast
Error message
could not cast
What it means
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'.
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
Example fix
// before let out = ca_i64.lhs_sub(1.5f64); // 1.5 not representable as i64 // after let out = ca_i64.cast(&DataType::Float64).unwrap().f64().unwrap().lhs_sub(1.5f64);
Defensive patterns
Strategy: validation
Validate before calling
fn scalar_fits<N: ToPrimitive + NumCast>(v: N) -> bool {
// check against the array's native type before calling lhs_*
// e.g. for i64 arrays: v.to_i64().is_some() && v.to_f64().map_or(true, |f| f.fract() == 0.0)
v.to_i64().is_some()
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- activate dtype
- can only convert date/datetime to NaiveDateTime
- can only convert date/datetime to NaiveTime
- cannot coerce datatypes
- cannot create date from other type. dtype
AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19).
Data as JSON: /api/errors/6b10d56526d9af7e.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-core/src/series/arithmetic/borrowed.rs:835
impl<T> Rem<T> for Series
where
T: Num + NumCast,
{
type Output = Self;
fn rem(self, rhs: T) -> Self::Output {
(&self).rem(rhs)
}
}
/// We cannot override the left hand side behaviour. So we create a trait LhsNumOps.
/// This allows for 1.add(&Series)
///
impl<T: PolarsNumericType> ChunkedArray<T> {
/// Apply lhs - self
#[must_use]
pub fn lhs_sub<N: Num + NumCast>(&self, lhs: N) -> Self {
let lhs: T::Native = NumCast::from(lhs).expect("could not cast");
ArithmeticChunked::wrapping_sub_scalar_lhs(lhs, self)
}
/// Apply lhs / self
#[must_use]
pub fn lhs_div<N: Num + NumCast>(&self, lhs: N) -> Self {
let lhs: T::Native = NumCast::from(lhs).expect("could not cast");
ArithmeticChunked::legacy_div_scalar_lhs(lhs, self)
}
/// Apply lhs % self
#[must_use]
pub fn lhs_rem<N: Num + NumCast>(&self, lhs: N) -> Self {
let lhs: T::Native = NumCast::from(lhs).expect("could not cast");
ArithmeticChunked::wrapping_mod_scalar_lhs(lhs, self)
}
}
View on GitHub (pinned to 9b5d73fd00)