pandas-dev/pandas · error · TypeError

values should be boolean numpy array. Use the 'pd.array'…

Error message

values should be boolean numpy array. Use the 'pd.array' function instead

What it means

Raised by BooleanArray.__init__ when values is not a numpy array of dtype bool_. BooleanArray is the low-level constructor expecting two aligned numpy bool arrays; using it with a Python list, an int array, or a pandas object triggers this guard. The message redirects users to pd.array(...) which handles coercion.

Solutions

  1. Use pd.array(values, dtype='boolean') for general construction — it runs coerce_to_array for you.
  2. If you must use BooleanArray directly, convert first: BooleanArray(np.asarray(values, dtype=bool), np.asarray(mask, dtype=bool)).
  3. For 0/1 integer arrays, run coerce_to_array to obtain (values, mask) then construct.

Example fix

// before
pd.BooleanArray([True, False, None], mask=[False, False, True])
// after
pd.array([True, False, None], dtype='boolean')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
assert isinstance(values, np.ndarray) and values.dtype == np.bool_, 'use pd.array(..., dtype="boolean") instead'

Type guard

def is_bool_ndarray(v) -> bool:
    import numpy as np
    return isinstance(v, np.ndarray) and v.dtype == np.bool_

Try / catch

try:
    pd.BooleanArray(values, mask)
except TypeError as e:
    if "should be boolean numpy array" in str(e):
        values = np.asarray(values, dtype=bool)
        ...

Prevention

When it happens

Trigger: Calling pd.BooleanArray([True, False], mask=...) directly; BooleanArray(np.array([1, 0]), mask=...); BooleanArray with a list, tuple, or ExtensionArray as values.

Common situations: Users reaching for the low-level BooleanArray constructor instead of the public pd.array(..., dtype='boolean'); passing integer 0/1 numpy arrays directly; copy-pasted code that assumed BooleanArray accepts list input.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/9976541a40e8865f. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/boolean.py:340

    <BooleanArray>
    [True, False, <NA>]
    Length: 3, dtype: boolean
    """

    _TRUE_VALUES = {"True", "TRUE", "true", "1", "1.0"}
    _FALSE_VALUES = {"False", "FALSE", "false", "0", "0.0"}

    @classmethod
    def _simple_new(cls, values: np.ndarray, mask: npt.NDArray[np.bool_]) -> Self:
        result = super()._simple_new(values, mask)
        result._dtype = BooleanDtype()
        return result

    def __init__(
        self, values: np.ndarray, mask: np.ndarray, copy: bool = False
    ) -> None:
        if not (isinstance(values, np.ndarray) and values.dtype == np.bool_):
            raise TypeError(
                "values should be boolean numpy array. Use "
                "the 'pd.array' function instead"
            )
        self._dtype = BooleanDtype()
        super().__init__(values, mask, copy=copy)

    @property
    def dtype(self) -> BooleanDtype:
        return self._dtype

    @classmethod
    def _from_sequence_of_strings(
        cls,
        strings: list[str],
        *,
        dtype: ExtensionDtype,
        copy: bool = False,
        true_values: list[str] | None = None,

View on GitHub (pinned to 3b7651241d)