pola-rs/polars · error

Cannot compare two series of different lengths.

Error message

Cannot compare two series of different lengths.

What it means

When comparing NullChunked series, get_broadcast_length only permits lengths to match or one side to have length 1 (broadcastable). Two null series with different lengths > 1 cannot be elementwise compared, so the code panics.

Solutions

  1. Ensure both series have the same length before comparing (slice/join on a key).
  2. Use join-based comparison (join on index) instead of positional elementwise comparison.
  3. If one side should broadcast, make it an actual length-1 series/literal.
  4. Check upstream data pipeline for where lengths diverge.

Example fix

// before
df.filter(col("a").eq(col("b"))) // a: len 3 null, b: len 5 null -> panic
// after
assert_eq!(df.height(), other.height()); // align first or join:
let out = df.with_row_index("i").join(other.with_row_index("i"), "i");
Defensive patterns

Strategy: validation

Validate before calling

# Polars (Python)
assert s_a.len() == s_b.len() or s_a.len() == 1 or s_b.len() == 1, \
    "cannot compare series of different lengths"

Type guard

fn comparable(lhs: &Series, rhs: &Series) -> bool {
    lhs.len() == rhs.len() || lhs.len() == 1 || rhs.len() == 1
}

Try / catch

// Rust
match lhs.equal(&rhs) {
    Ok(mask) => mask,
    Err(_) => /* align lengths or join on index, then compare */,
}

Prevention

When it happens

Trigger: Elementwise comparison operators (==, !=, >, >=, etc.) between two all-null Series of differing lengths, neither of which is length 1 — e.g. df.filter(col('a') == col('b')) where both are null-typed with len 3 vs 5.

Common situations: Comparing columns from differently-sized frames/concatenations, or comparing against an all-null literal column of mismatched length.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/a43e2287167d2215. Report an issue: GitHub.

Appendix: 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 fe841f959e)