{"record":{"id":"6d884b4c933086da","repo":"pola-rs/polars","slug":"cannot-apply-operation-on-arrays-of-different-lengths","errorCode":null,"errorMessage":"Cannot apply operation on arrays of different lengths","messagePattern":"Cannot apply operation on arrays of different lengths","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/chunked_array/ops/arity.rs","lineNumber":889,"sourceCode":"                    ChunkedArray::<O>::with_chunk(lhs.name().clone(), arr)\n                },\n                Some(rhs) => unary_kernel(lhs, |arr| rhs_broadcast_kernel(arr, rhs.clone())),\n            }\n        },\n        (1, _) => {\n            let opt_lhs = lhs.get(0);\n            match opt_lhs {\n                None => {\n                    let arr = O::Array::full_null(\n                        rhs.len(),\n                        O::get_static_dtype().to_arrow(CompatLevel::newest()),\n                    );\n                    ChunkedArray::<O>::with_chunk(lhs.name().clone(), arr)\n                },\n                Some(lhs) => unary_kernel(rhs, |arr| lhs_broadcast_kernel(lhs.clone(), arr)),\n            }\n        },\n        _ => panic!(\"Cannot apply operation on arrays of different lengths\"),\n    };\n    out.with_name(name.clone())\n}\n\npub fn apply_binary_kernel_broadcast_owned<L, R, O, K, LK, RK>(\n    lhs: ChunkedArray<L>,\n    rhs: ChunkedArray<R>,\n    kernel: K,\n    lhs_broadcast_kernel: LK,\n    rhs_broadcast_kernel: RK,\n) -> ChunkedArray<O>\nwhere\n    L: PolarsDataType,\n    R: PolarsDataType,\n    O: PolarsDataType,\n    K: Fn(L::Array, R::Array) -> O::Array,\n    for<'a> LK: Fn(L::Physical<'a>, R::Array) -> O::Array,\n    for<'a> RK: Fn(L::Array, R::Physical<'a>) -> O::Array,","sourceCodeStart":871,"sourceCodeEnd":907,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/chunked_array/ops/arity.rs#L871-L907","documentation":"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.","triggerScenarios":"Calling any wrapping_* arithmetic (or floor/trunc div, mod) on two Series/ChunkedArrays whose lengths differ and where neither side has length 1.","commonSituations":"Arithmetic between a column and a misaligned slice of another column, joining/merging mismatches, or after a filter applied to only one operand.","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"],"exampleFix":"// before\nlet c = (&a + &b); // a.len()==5, b.len()==3 -> panic\n// after\nassert_eq!(a.len(), b.len());\nlet c = (&a + &b);","handlingStrategy":"validation","validationCode":"fn broadcastable(a_len: usize, b_len: usize) -> bool {\n    a_len == b_len || a_len == 1 || b_len == 1\n}\nif !broadcastable(lhs.len(), rhs.len()) { return Err(...); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["rust","polars","shape-mismatch","broadcast"],"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"}