{"record":{"id":"a43e2287167d2215","repo":"pola-rs/polars","slug":"cannot-compare-two-series-of-different-lengths","errorCode":null,"errorMessage":"Cannot compare two series of different lengths.","messagePattern":"Cannot compare two series of different lengths\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/chunked_array/comparison/mod.rs","lineNumber":243,"sourceCode":"        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))\n    }\n\n    fn lt(&self, rhs: &NullChunked) -> Self::Item {\n        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))\n    }\n\n    fn lt_eq(&self, rhs: &NullChunked) -> Self::Item {\n        BooleanChunked::full_null(self.name().clone(), get_broadcast_length(self, rhs))\n    }\n}\n\n#[inline]\nfn get_broadcast_length(lhs: &NullChunked, rhs: &NullChunked) -> usize {\n    match (lhs.len(), rhs.len()) {\n        (1, len_r) => len_r,\n        (len_l, 1) => len_l,\n        (len_l, len_r) if len_l == len_r => len_l,\n        _ => panic!(\"Cannot compare two series of different lengths.\"),\n    }\n}\n\nimpl ChunkCompareEq<&BooleanChunked> for BooleanChunked {\n    type Item = BooleanChunked;\n\n    fn equal(&self, rhs: &BooleanChunked) -> BooleanChunked {\n        // Broadcast.\n        match (self.len(), rhs.len()) {\n            (_, 1) => {\n                if let Some(value) = rhs.get(0) {\n                    arity::unary_mut_values(self, |arr| arr.tot_eq_kernel_broadcast(&value).into())\n                } else {\n                    BooleanChunked::full_null(PlSmallStr::EMPTY, self.len())\n                }\n            },\n            (1, _) => {\n                if let Some(value) = self.get(0) {","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/chunked_array/comparison/mod.rs#L225-L261","documentation":"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.","triggerScenarios":"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.","commonSituations":"Comparing columns from differently-sized frames/concatenations, or comparing against an all-null literal column of mismatched length.","solutions":["Ensure both series have the same length before comparing (slice/join on a key).","Use join-based comparison (join on index) instead of positional elementwise comparison.","If one side should broadcast, make it an actual length-1 series/literal.","Check upstream data pipeline for where lengths diverge."],"exampleFix":"// before\ndf.filter(col(\"a\").eq(col(\"b\"))) // a: len 3 null, b: len 5 null -> panic\n// after\nassert_eq!(df.height(), other.height()); // align first or join:\nlet out = df.with_row_index(\"i\").join(other.with_row_index(\"i\"), \"i\");","handlingStrategy":"validation","validationCode":"# Polars (Python)\nassert s_a.len() == s_b.len() or s_a.len() == 1 or s_b.len() == 1, \\\n    \"cannot compare series of different lengths\"","typeGuard":"fn comparable(lhs: &Series, rhs: &Series) -> bool {\n    lhs.len() == rhs.len() || lhs.len() == 1 || rhs.len() == 1\n}","tryCatchPattern":"// Rust\nmatch lhs.equal(&rhs) {\n    Ok(mask) => mask,\n    Err(_) => /* align lengths or join on index, then compare */,\n}","preventionTips":["Verify frame heights/column lengths before elementwise comparisons.","Align data via join on a key or row index instead of positional comparison.","Use length-1 literals when broadcasting is intended.","Add assertions in pipelines that merge/concat steps preserve equal lengths."],"tags":["rust","polars","comparison","length-mismatch"],"backgroundTag":"shape-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}