pola-rs/polars · error
Cannot compare two series of different lengths.
Error message
Cannot compare two series of different lengths.
What it means
Panic in get_broadcast_length when comparing two NullChunked (or series) of incompatible lengths: neither side has length 1 (broadcastable) and the lengths differ, so element-wise comparison is undefined. Align the lengths or broadcast one side.
Source
Thrown at crates/polars-core/src/chunked_array/comparison/mod.rs:243
BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
}
fn lt(&self, rhs: &NullChunked) -> Self::Item {
BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
}
fn lt_eq(&self, rhs: &NullChunked) -> Self::Item {
BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))
}
}
#[inline]
fn get_broadcast_length(lhs: &NullChunked, rhs: &NullChunked) -> usize {
match (lhs.len(), rhs.len()) {
(1, len_r) => len_r,
(len_l, 1) => len_l,
(len_l, len_r) if len_l == len_r => len_l,
_ => panic!("Cannot compare two series of different lengths."),
}
}
impl ChunkCompareEq<&BooleanChunked> for BooleanChunked {
type Item = BooleanChunked;
fn equal(&self, rhs: &BooleanChunked) -> BooleanChunked {
// Broadcast.
match (self.len(), rhs.len()) {
(_, 1) => {
if let Some(value) = rhs.get(0) {
arity::unary_mut_values(self, |arr| arr.tot_eq_kernel_broadcast(&value).into())
} else {
BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())
}
},
(1, _) => {
if let Some(value) = self.get(0) {View on GitHub (pinned to 9b5d73fd00)
Solutions
- Compare only series of equal length; check `.len()` on both sides before the comparison.
- Reindex, join, or zip the two series so they align row-by-row before comparing.
- If one side is meant as a scalar, wrap it with `lit()` or broadcast it explicitly.
Defensive patterns
Strategy: validation
When it happens
Trigger: This panic/expect fires when execution reaches an unguarded state described by: "Cannot compare two series of different lengths.". Typical triggers: unsupported dtype or feature-gated code path reached at runtime, invalid user input or environment variable value, calling API methods in the wrong order or on mismatched types, or data (lengths, offsets, ranges) violating the function's preconditions.
Common situations: Encountered when input data or configuration does not meet the preconditions of the throwing code path ("Cannot compare two series of different lengths."). Common cases: missing Cargo feature flags, mismatched dtypes/lengths between arrays or series, out-of-range temporal values, malformed environment variables, or operations on unsupported/complex nested types.
AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19).
Data as JSON: /api/errors/a43e2287167d2215.
Report an issue: GitHub.