{"record":{"id":"6178602d6a054e1e","repo":"pandas-dev/pandas","slug":"values-shape-must-match-mask-shape","errorCode":null,"errorMessage":"values.shape must match mask.shape","messagePattern":"values\\.shape must match mask\\.shape","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/masked.py","lineNumber":159,"sourceCode":"\n    @classmethod\n    def _simple_new(cls, values: np.ndarray, mask: npt.NDArray[np.bool_]) -> Self:\n        result = BaseMaskedArray.__new__(cls)\n        result._data = values\n        result._mask = mask\n        return result\n\n    def __init__(\n        self, values: np.ndarray, mask: npt.NDArray[np.bool_], copy: bool = False\n    ) -> None:\n        # values is supposed to already be validated in the subclass\n        if not (isinstance(mask, np.ndarray) and mask.dtype == np.bool_):\n            raise TypeError(\n                \"mask should be boolean numpy array. Use \"\n                \"the 'pd.array' function instead\"\n            )\n        if values.shape != mask.shape:\n            raise ValueError(\"values.shape must match mask.shape\")\n\n        if copy:\n            values = values.copy()\n            mask = mask.copy()\n\n        self._data = values\n        self._mask = mask\n\n    @classmethod\n    def _from_sequence(cls, scalars, *, dtype=None, copy: bool = False) -> Self:\n        values, mask = cls._coerce_to_array(scalars, dtype=dtype, copy=copy)\n        return cls(values, mask)\n\n    def _cast_pointwise_result(self, values) -> ArrayLike:\n        if isna(values).all():\n            return type(self)._from_sequence(values, dtype=self.dtype)\n        if not (isinstance(values, np.ndarray) and values.dtype == object):\n            values = construct_1d_object_array_from_listlike(values)","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/masked.py#L141-L177","documentation":"Raised in BaseMaskedArray.__init__ when values.shape != mask.shape. The data array and the boolean mask must be element-aligned; mismatched shapes are rejected to prevent silent misalignment of NA markers.","triggerScenarios":"IntegerArray(values, mask) where len(values) != len(mask); constructing with mask derived from a differently-sized array.","commonSituations":"Mismatched lengths from independent computations; off-by-one in mask construction; broadcasting mistakes.","solutions":["Ensure the mask is generated from the same axis/length as the values.","Broadcast or truncate intentionally before constructing, and assert equality.","Prefer pd.array(...) which derives the mask consistently from the input."],"exampleFix":"// before\nIntegerArray(values[:5], mask[:4])\n// after\nassert values.shape == mask.shape\nIntegerArray(values, mask)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef build_masked_inputs(values, mask):\n    values = np.asarray(values)\n    mask = np.asarray(mask, dtype=bool)\n    assert values.shape == mask.shape, (values.shape, mask.shape)\n    return values, mask","typeGuard":"def shapes_match(values, mask):\n    return values.shape == mask.shape","tryCatchPattern":null,"preventionTips":["Always derive the mask from the same axis/length as the values.","Assert shape equality before constructing a masked array.","Prefer pd.array(...) which derives the mask consistently from input."],"tags":["masked-array","shape-mismatch","constructor"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}