{"record":{"id":"b15da83435fb383e","repo":"pandas-dev/pandas","slug":"lengths-of-operands-do-not-match-len-self-b15da8","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/string_.py","lineNumber":1250,"sourceCode":"\n        mask = isna(self) | isna(other)\n        valid = ~mask\n\n        if lib.is_list_like(other):\n            if not isinstance(\n                other, (list, ExtensionArray, np.ndarray)\n            ) and not ops.has_castable_attr(other):\n                warnings.warn(\n                    f\"Operation with {type(other).__name__} is deprecated. \"\n                    \"In a future version these will be treated as scalar-like. \"\n                    \"To retain the old behavior, explicitly wrap in a Series \"\n                    \"instead.\",\n                    Pandas4Warning,\n                    stacklevel=find_stack_level(),\n                )\n            if len(other) != len(self):\n                # prevent improper broadcasting when other is 2D\n                raise ValueError(\n                    f\"Lengths of operands do not match: {len(self)} != {len(other)}\"\n                )\n\n            # for array-likes, first filter out NAs before converting to numpy\n            if not is_array_like_deprecate_non_pandas(other):\n                other = np.asarray(other)\n            other = other[valid]\n\n        other_dtype = getattr(other, \"dtype\", None)\n        if op.__name__.strip(\"_\") in [\"mul\", \"rmul\"] and (\n            lib.is_bool(other) or lib.is_np_dtype(other_dtype, \"b\")\n        ):\n            # GH#62595\n            raise TypeError(\n                \"Cannot multiply StringArray by bools. \"\n                \"Explicitly cast to integers instead.\"\n            )\n","sourceCodeStart":1232,"sourceCodeEnd":1268,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L1232-L1268","documentation":"StringArray._cmp_method performs element-wise comparison/arithmetic between self and a list-like `other`. Before operating it asserts len(other) == len(self); a mismatch raises ValueError showing both lengths. This prevents silent mis-broadcasting of 2-D or wrongly-sized operands.","triggerScenarios":"Calling string_array + ['a', 'b'] when lengths differ, string_array == other_array of a different length, or any element-wise op between a StringArray and a list/ndarray/ExtensionArray whose length does not match.","commonSituations":"Misaligned operands in vectorized ops; broadcasting a short list expecting repetition; comparing columns from different DataFrames without alignment.","solutions":["Match the operand length to the array length, or pass a scalar.","Align via a pandas Series/Index so broadcasting is explicit.","Broadcast manually: ['a'] * len(arr) if you want repetition."],"exampleFix":"// before\nstring_array + ['a']\n\n// after\nstring_array + 'a'","handlingStrategy":"validation","validationCode":"import numpy as np\nfrom pandas._libs import lib\n\nif lib.is_list_like(other) and len(other) != len(string_array):\n    other = other * (len(string_array) // len(other)) if len(other) == 1 else other\nresult = string_array + other","typeGuard":"def lengths_match(arr, other) -> bool:\n    try:\n        return len(other) == len(arr)\n    except TypeError:\n        return True  # scalar","tryCatchPattern":null,"preventionTips":["Match operand lengths for element-wise string operations, or use a scalar.","Align via pandas Series when operands come from different sources.","Broadcast explicitly with [v] * len(arr) when repetition is intended."],"tags":["string-array","comparison","broadcasting","shape"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}