pola-rs/polars · error
Cannot apply operation on arrays of different lengths
Error message
Cannot apply operation on arrays of different lengths
What it means
apply_binary_kernel_broadcast implements element-wise binary kernels (wrapping_add, wrapping_sub, wrapping_mul, floor/trunc div, mod) between two ChunkedArrays. It supports lhs length 1 (broadcast lhs over rhs), rhs length 1 (broadcast rhs over lhs), or equal lengths; anything else has no defined element pairing, so it panics.
Solutions
- Ensure both operands have the same length before the operation, or that the shorter side has exactly length 1
- Check for upstream filters/slices/head/tail that shrank only one of the two series
- Use lit(...) for scalar operands so they are broadcast explicitly
Example fix
// before let c = (&a + &b); // a.len()==5, b.len()==3 -> panic // after assert_eq!(a.len(), b.len()); let c = (&a + &b);
Defensive patterns
Strategy: validation
Validate before calling
fn broadcastable(a_len: usize, b_len: usize) -> bool {
a_len == b_len || a_len == 1 || b_len == 1
}
if !broadcastable(lhs.len(), rhs.len()) { return Err(...); } Prevention
- Assert equal lengths right after producing both operands
- Use lit() for scalars instead of one-row series when unsure
- Apply filters to both sides of a binary op symmetrically
When it happens
Trigger: Calling any wrapping_* arithmetic (or floor/trunc div, mod) on two Series/ChunkedArrays whose lengths differ and where neither side has length 1.
Common situations: Arithmetic between a column and a misaligned slice of another column, joining/merging mismatches, or after a filter applied to only one operand.
Related errors
- {0}
- activate dtype
- activate dtype-categorical to convert dictionary arrays
- activate ' ' feature
- activate feature dtype-date
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/6d884b4c933086da.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-core/src/chunked_array/ops/arity.rs:889
ChunkedArray::<O>::with_chunk(lhs.name().clone(), arr)
},
Some(rhs) => unary_kernel(lhs, |arr| rhs_broadcast_kernel(arr, rhs.clone())),
}
},
(1, _) => {
let opt_lhs = lhs.get(0);
match opt_lhs {
None => {
let arr = O::Array::full_null(
rhs.len(),
O::get_static_dtype().to_arrow(CompatLevel::newest()),
);
ChunkedArray::<O>::with_chunk(lhs.name().clone(), arr)
},
Some(lhs) => unary_kernel(rhs, |arr| lhs_broadcast_kernel(lhs.clone(), arr)),
}
},
_ => panic!("Cannot apply operation on arrays of different lengths"),
};
out.with_name(name.clone())
}
pub fn apply_binary_kernel_broadcast_owned<L, R, O, K, LK, RK>(
lhs: ChunkedArray<L>,
rhs: ChunkedArray<R>,
kernel: K,
lhs_broadcast_kernel: LK,
rhs_broadcast_kernel: RK,
) -> ChunkedArray<O>
where
L: PolarsDataType,
R: PolarsDataType,
O: PolarsDataType,
K: Fn(L::Array, R::Array) -> O::Array,
for<'a> LK: Fn(L::Physical<'a>, R::Array) -> O::Array,
for<'a> RK: Fn(L::Array, R::Physical<'a>) -> O::Array,View on GitHub (pinned to fe841f959e)