{"record":{"id":"b31eb84ac7c3142c","repo":"pandas-dev/pandas","slug":"lengths-of-operands-do-not-match-len-self","errorCode":null,"errorMessage":"Lengths of operands do not match: {len(self)} != {len(other)}","messagePattern":"Lengths of operands do not match: (.+?) != (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":1265,"sourceCode":"                f\"'{op_name}' operations between boolean dtype and {self.dtype} are \"\n                \"deprecated and will raise in a future version. Explicitly \"\n                \"cast the strings to a boolean dtype before operating instead.\",\n                Pandas4Warning,\n                stacklevel=find_stack_level(),\n            )\n            return op(other, self.astype(bool))\n        else:\n            return self._evaluate_op_method(other, op, ARROW_LOGICAL_FUNCS)\n\n    def _str_arith_method_object_fallback(\n        self, other, op\n    ) -> Self | npt.NDArray[np.object_]:\n        mask = isna(self) | isna(other)\n        valid = ~mask\n\n        if is_list_like(other):\n            if len(other) != len(self):\n                raise ValueError(\n                    f\"Lengths of operands do not match: {len(self)} != {len(other)}\"\n                )\n            if not is_array_like_deprecate_non_pandas(other):\n                other = np.asarray(other)\n            other = other[valid]\n\n        result = np.empty(len(self), dtype=object)\n        result[mask] = self.dtype.na_value\n        result[valid] = op(np.asarray(self, dtype=object)[valid], other)\n\n        if not lib.is_string_array(result, skipna=True):\n            return result\n        return type(self)._from_sequence(result, dtype=self.dtype)\n\n    def _arith_method(self, other, op) -> Self | npt.NDArray[np.object_]:\n        if isinstance(other, BaseOffset) and pa.types.is_date(self._pa_array.type):\n            # Cast date32/date64 → timestamp, apply offset via DatetimeArray, cast back\n            ts_array = type(self)(self._pa_array.cast(pa.timestamp(\"us\")))","sourceCodeStart":1247,"sourceCodeEnd":1283,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L1247-L1283","documentation":"Raised by _str_arith_method_object_fallback when `other` is list-like and its length differs from len(self). This fallback path handles string arithmetic that pyarrow rejected (ArrowInvalid/TypeError) by operating elementwise in object dtype; it requires aligned lengths. The check at line 1264 enforces the contract before indexing with the validity mask.","triggerScenarios":"String arithmetic fallback with mismatched lengths: `s1 + s2` where len(s1) != len(s2) and pyarrow already rejected the op so the object fallback runs. Also `s + [1,2]` against a length-3 string array.","commonSituations":"Concatenating columns from misaligned frames (different filters applied), broadcasting mistakes, or passing a short list expecting scalar broadcast (which the fallback does not do).","solutions":["Align indexes/lengths before the op: reset_index or reindex to match.","Broadcast a scalar instead of a list: s + sep (str), not s + [sep].","Validate len(other) == len(s) up front.","Use Series with aligned index so pandas handles broadcasting."],"exampleFix":"# before\nout = firsts + rests   # len 5 vs len 4 -> ValueError in fallback\n# after\nrests = rests.reindex(firsts.index)\nout = firsts + rests","handlingStrategy":"validation","validationCode":"def aligned_str_op(left, right, op):\n    import numpy as np\n    if hasattr(right, '__len__') and not isinstance(right, str):\n        if len(right) != len(left):\n            raise ValueError(f'length mismatch {len(left)} != {len(right)}')\n    return op(left, right)\n\nimport operator\nout = aligned_str_op(firsts, rests, operator.add)","typeGuard":"def is_length_aligned(other, target_len) -> bool:\n    if isinstance(other, str) or not hasattr(other, '__len__'):\n        return True  # scalar-like\n    return len(other) == target_len","tryCatchPattern":"try:\n    out = a + b\nexcept ValueError as e:\n    if 'Lengths of operands do not match' in str(e):\n        # align via index or broadcast scalar\n        out = a + pd.Series(b, index=a.index)\n    else:\n        raise","preventionTips":["Align Series indexes before binary ops.","Broadcast scalars (not length-1 lists) for scalar semantics.","Validate len(other) == len(self) for array operands."],"tags":["pyarrow","arithmetic","length-mismatch","string-dtype"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}