{"record":{"id":"9976541a40e8865f","repo":"pandas-dev/pandas","slug":"values-should-be-boolean-numpy-array-use-the-pd","errorCode":null,"errorMessage":"values should be boolean numpy array. Use the 'pd.array' function instead","messagePattern":"values should be boolean numpy array\\. Use the 'pd\\.array' function instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/boolean.py","lineNumber":340,"sourceCode":"    <BooleanArray>\n    [True, False, <NA>]\n    Length: 3, dtype: boolean\n    \"\"\"\n\n    _TRUE_VALUES = {\"True\", \"TRUE\", \"true\", \"1\", \"1.0\"}\n    _FALSE_VALUES = {\"False\", \"FALSE\", \"false\", \"0\", \"0.0\"}\n\n    @classmethod\n    def _simple_new(cls, values: np.ndarray, mask: npt.NDArray[np.bool_]) -> Self:\n        result = super()._simple_new(values, mask)\n        result._dtype = BooleanDtype()\n        return result\n\n    def __init__(\n        self, values: np.ndarray, mask: np.ndarray, copy: bool = False\n    ) -> None:\n        if not (isinstance(values, np.ndarray) and values.dtype == np.bool_):\n            raise TypeError(\n                \"values should be boolean numpy array. Use \"\n                \"the 'pd.array' function instead\"\n            )\n        self._dtype = BooleanDtype()\n        super().__init__(values, mask, copy=copy)\n\n    @property\n    def dtype(self) -> BooleanDtype:\n        return self._dtype\n\n    @classmethod\n    def _from_sequence_of_strings(\n        cls,\n        strings: list[str],\n        *,\n        dtype: ExtensionDtype,\n        copy: bool = False,\n        true_values: list[str] | None = None,","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/boolean.py#L322-L358","documentation":"BooleanArray.__init__ (boolean.py:340) requires `values` to be a numpy ndarray with dtype np.bool_; anything else (a list, an int array, a Python bool) raises TypeError directing the user to pd.array. The constructor is a low-level API; the two-array (data+mask) representation is an invariant that must be honored by callers.","triggerScenarios":"Directly instantiating pd.arrays.BooleanArray([True, False], mask) with a Python list or a non-bool ndarray instead of using pd.array(...).","commonSituations":"Copy-pasted examples that call BooleanArray(...) directly; library code that tried to skip the public constructor; misunderstandings of the public API surface.","solutions":["Use the public constructor: pd.array([True, False, None], dtype='boolean').","If you must use BooleanArray directly, first convert: np.asarray(values, dtype=bool).","Provide a correctly-shaped mask matching the bool ndarray.","Avoid the low-level constructor in application code; it is intended for EA internals."],"exampleFix":"# before\nfrom pandas.arrays import BooleanArray\nba = BooleanArray([True, False], mask=[False, False])  # raises\n\n# after\nimport numpy as np\nba = BooleanArray(np.array([True, False], dtype=bool), np.array([False, False], dtype=bool))\n# or preferably\nba = pd.array([True, False], dtype=\"boolean\")","handlingStrategy":"validation","validationCode":"def make_boolean_array(values, mask=None):\n    import numpy as np\n    values = np.asarray(values, dtype=bool)\n    if mask is None:\n        mask = np.zeros(values.shape, dtype=bool)\n    else:\n        mask = np.asarray(mask, dtype=bool)\n    from pandas.core.arrays.boolean import BooleanArray\n    return BooleanArray(values, mask)","typeGuard":"def is_bool_ndarray(x) -> bool:\n    import numpy as np\n    return isinstance(x, np.ndarray) and x.dtype == np.bool_","tryCatchPattern":"try:\n    from pandas.arrays import BooleanArray\n    ba = BooleanArray(values, mask)\nexcept TypeError as e:\n    if \"pd.array\" in str(e):\n        ba = pd.array(values, dtype=\"boolean\")\n    else:\n        raise","preventionTips":["Prefer pd.array([...], dtype='boolean') over the BooleanArray constructor","Convert values to np.bool_ ndarray if using the low-level constructor","Provide a bool mask of matching shape"],"tags":["boolean","constructor","api-misuse","ndarray"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}