{"record":{"id":"c762a3e7dc84f9f9","repo":"pandas-dev/pandas","slug":"values-shape-and-mask-shape-must-match","errorCode":null,"errorMessage":"values.shape and mask.shape must match","messagePattern":"values\\.shape and mask\\.shape must match","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/boolean.py","lineNumber":262,"sourceCode":"        ):\n            raise TypeError(\"Need to pass bool-like values\")\n\n    if mask is None and mask_values is None:\n        mask = np.zeros(values.shape, dtype=bool)\n    elif mask is None:\n        mask = mask_values\n    elif isinstance(mask, np.ndarray) and mask.dtype == np.bool_:\n        if mask_values is not None:\n            mask = mask | mask_values\n        elif copy:\n            mask = mask.copy()\n    else:\n        mask = np.array(mask, dtype=bool)\n        if mask_values is not None:\n            mask = mask | mask_values\n\n    if values.shape != mask.shape:\n        raise ValueError(\"values.shape and mask.shape must match\")\n\n    return values, mask\n\n\n@set_module(\"pandas.arrays\")\nclass BooleanArray(BaseMaskedArray):\n    \"\"\"\n    Array of boolean (True/False) data with missing values.\n\n    This is a pandas Extension array for boolean data, under the hood\n    represented by 2 numpy arrays: a boolean array with the data and\n    a boolean array with the mask (True indicating missing).\n\n    BooleanArray implements Kleene logic (sometimes called three-value\n    logic) for logical operations. See :ref:`boolean.kleene` for more.\n\n    To construct a BooleanArray from generic array-like input, use\n    :func:`pandas.array` specifying ``dtype=\"boolean\"`` (see examples","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/boolean.py#L244-L280","documentation":"Raised by coerce_to_array at the end of normalization when the final values array and the final mask array have different shapes. The mask is derived either from the input, from isna(values), from the user-supplied mask, or from a combination; any path producing a mismatch trips this guard before construction.","triggerScenarios":"pd.array(values, mask=shorter_mask, dtype='boolean') where mask has a different length than values; passing a BooleanArray-derived values with an external mask whose length differs; supplying mask as a 2D array while values is 1D.","commonSituations":"Off-by-one in user-computed masks; masks computed against a filtered/extended version of the values; broadcasting mistakes where mask is scalar-shaped; reshaping values without reshaping mask.","solutions":["Verify mask.shape == values.shape before calling: assert mask.shape == values.shape.","Recompute the mask from the same values used: mask = isna(values).","Align lengths by re-indexing both arrays to a common index."],"exampleFix":"// before\npd.array(np.array([True, False, True]), mask=np.array([False, True]), dtype='boolean')\n// after\nvalues = np.array([True, False, True])\nmask = np.zeros(len(values), dtype=bool)\nmask[1] = True\npd.array(values, mask=mask, dtype='boolean')","handlingStrategy":"validation","validationCode":"assert np.asarray(values).shape == np.asarray(mask).shape, 'values and mask shape mismatch'","typeGuard":"def shapes_match(values, mask) -> bool:\n    import numpy as np\n    return np.asarray(values).shape == np.asarray(mask).shape","tryCatchPattern":"try:\n    pd.array(values, mask=mask, dtype='boolean')\nexcept ValueError as e:\n    if 'shape' in str(e):\n        mask = np.zeros(len(values), dtype=bool)\n        ...","preventionTips":["Always derive mask length from values length.","Recompute mask = isna(values) for consistency."],"tags":["boolean","mask","shape","coerce","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}