{"record":{"id":"18c27b85705bf09a","repo":"pandas-dev/pandas","slug":"can-only-perform-ops-with-1-d-structures","errorCode":null,"errorMessage":"can only perform ops with 1-d structures","messagePattern":"can only perform ops with 1-d structures","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/masked.py","lineNumber":984,"sourceCode":"            )\n\n        if (\n            not hasattr(other, \"dtype\")\n            and is_list_like(other)\n            and len(other) == len(self)\n        ):\n            # Try inferring masked dtype instead of casting to object\n            other = pd_array(other)\n            other = extract_array(other, extract_numpy=True)\n\n        if isinstance(other, BaseMaskedArray):\n            other, omask = other._data, other._mask\n\n        elif is_list_like(other):\n            if not isinstance(other, ExtensionArray):\n                other = np.asarray(other)\n            if other.ndim > 1:\n                raise NotImplementedError(\"can only perform ops with 1-d structures\")\n\n        # We wrap the non-masked arithmetic logic used for numpy dtypes\n        #  in Series/Index arithmetic ops.\n        other = ops.maybe_prepare_scalar_for_op(other, (len(self),))\n        pd_op = ops.get_array_op(op)\n        other = ensure_wrapped_if_datetimelike(other)\n\n        if isinstance(other, ExtensionArray) and isinstance(other.dtype, ArrowDtype):\n            # GH#58602\n            return NotImplemented\n\n        if op_name in {\"pow\", \"rpow\"} and isinstance(other, np.bool_):\n            # Avoid DeprecationWarning: In future, it will be an error\n            #  for 'np.bool_' scalars to be interpreted as an index\n            #  e.g. test_array_scalar_like_equivalence\n            other = bool(other)\n\n        mask = self._propagate_mask(omask, other)","sourceCodeStart":966,"sourceCodeEnd":1002,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/masked.py#L966-L1002","documentation":"Raised by BaseMaskedArray._arith_method when the 'other' operand is a list-like (ndarray or ExtensionArray) with more than one dimension. Masked array arithmetic is only defined element-wise against scalars or 1-D structures of matching length; 2-D inputs are rejected because broadcasting semantics are not implemented at this layer.","triggerScenarios":"Computing masked_array + two_dimensional_ndarray, or any binary op (+, -, *, /, comparison via _arith_method) where other.ndim > 1 after np.asarray conversion.","commonSituations":"Broadcasting a column against a matrix during feature engineering; passing a 2-D numpy array where a 1-D was expected; mis-shaped joins or concatenations feeding into arithmetic.","solutions":["Reshape the operand to 1-D: other.ravel(), other.flatten(), or other[:, 0].","Use a DataFrame to perform aligned 2-D operations instead of array-level arithmetic.","Broadcast explicitly by iterating columns and combining results."],"exampleFix":"// before\narr + matrix   # matrix.ndim == 2 -> raises\n// after\narr + matrix[:, 0]   # 1-D operand","handlingStrategy":"validation","validationCode":"import numpy as np\nother_arr = np.asarray(other)\nif other_arr.ndim > 1:\n    other = other_arr.reshape(other_arr.shape[0])\nresult = arr + other","typeGuard":"def is_1d_or_scalar(other) -> bool:\n    import numpy as np\n    if np.isscalar(other):\n        return True\n    return hasattr(other, 'ndim') and other.ndim == 1","tryCatchPattern":"try:\n    result = arr + other\nexcept NotImplementedError as e:\n    if '1-d structures' in str(e):\n        result = arr + np.asarray(other).reshape(-1)\n    else:\n        raise","preventionTips":["Reshape operands to 1-D before masked-array arithmetic.","Use DataFrames for 2-D broadcasting-heavy workflows.","Validate other.ndim before dispatching to array arithmetic."],"tags":["pandas","masked-array","arithmetic","ndim","broadcasting"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}