{"record":{"id":"f98fdb3b63ad5c36","repo":"pandas-dev/pandas","slug":"mask-must-be-a-1d-list-like","errorCode":null,"errorMessage":"mask must be a 1D list-like","messagePattern":"mask must be a 1D list-like","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/numeric.py","lineNumber":215,"sourceCode":"            if is_nan_na():\n                mask = np.isnan(values)\n            else:\n                mask = np.zeros(len(values), dtype=np.bool_)\n                if dtype_cls.__name__.strip(\"_\").startswith((\"I\", \"U\")):\n                    wrong = np.isnan(values)\n                    if wrong.any():\n                        raise ValueError(\"Cannot cast NaN value to Integer dtype.\")\n        elif is_nan_na():\n            mask = libmissing.is_numeric_na(values)\n        else:\n            # is_numeric_na will raise on non-numeric NAs\n            libmissing.is_numeric_na(values)\n            mask = libmissing.is_pdna_or_none(values)\n    else:\n        assert len(mask) == len(values)\n\n    if mask.ndim != 1:\n        raise TypeError(\"mask must be a 1D list-like\")\n\n    # infer dtype if needed\n    if dtype is None:\n        dtype = default_dtype\n    else:\n        dtype = dtype.numpy_dtype\n\n    if is_integer_dtype(dtype) and values.dtype.kind == \"f\" and len(values) > 0:\n        if mask.all():\n            values = np.ones(values.shape, dtype=dtype)\n        else:\n            idx = np.nanargmax(values)\n            if int(values[idx]) != original[idx]:\n                # We have ints that lost precision during the cast.\n                inferred_type = lib.infer_dtype(original, skipna=True)\n                if (\n                    inferred_type not in [\"floating\", \"mixed-integer-float\"]\n                    and not mask.any()","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/numeric.py#L197-L233","documentation":"Raised by _coerce_to_data_and_mask when an explicitly supplied mask has ndim != 1. The mask must be a 1-D boolean array aligned elementwise with values; a 2-D or scalar mask is rejected because there is no defined correspondence to the 1-D data.","triggerScenarios":"Constructing NumericArray(values, mask) where mask is a 2-D boolean array, or passing a multi-dimensional mask through pd.array / from_arrow internal paths.","commonSituations":"Building a masked array manually with a mask sliced from a 2-D structure; reshaping masks incorrectly during ETL.","solutions":["Flatten the mask to 1-D matching len(values): mask = mask.ravel().","Select the corresponding column of the mask: mask = mask_2d[:, col_index].","Re-derive the mask from values via np.isnan/np.isna after ensuring 1-D data."],"exampleFix":"// before\nNumericArray(vals, mask_2d)  # raises: mask must be a 1D list-like\n\n// after\nNumericArray(vals, mask_2d.ravel())","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef ensure_1d_mask(mask, n):\n    m = np.asarray(mask, dtype=bool)\n    if m.ndim != 1:\n        raise TypeError(f\"mask must be 1D, got ndim={m.ndim}\")\n    if m.shape[0] != n:\n        raise ValueError(\"mask length must match values\")\n    return m","typeGuard":"def is_1d_mask(mask) -> bool:\n    import numpy as np\n    return np.asarray(mask).ndim == 1","tryCatchPattern":"try:\n    arr = NumericArray(values, mask)\nexcept TypeError as e:\n    if \"mask must be a 1D list-like\" in str(e):\n        import numpy as np\n        arr = NumericArray(values, np.asarray(mask, dtype=bool).ravel())\n    else:\n        raise","preventionTips":["Always build masks as 1-D boolean arrays of length len(values).","Flatten masks sliced from 2-D structures.","Prefer pd.array(...) which constructs the mask internally."],"tags":["numeric-array","mask","ndim","validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}