pandas-dev/pandas · error · TypeError
values should be boolean numpy array. Use the 'pd.array' fun
Error message
values should be boolean numpy array. Use the 'pd.array' function instead
What it means
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.
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 71959b8cb9)
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.
Example fix
# before from pandas.arrays import BooleanArray ba = BooleanArray([True, False], mask=[False, False]) # raises # after import numpy as np ba = BooleanArray(np.array([True, False], dtype=bool), np.array([False, False], dtype=bool)) # or preferably ba = pd.array([True, False], dtype="boolean")
Defensive patterns
Strategy: validation
Validate before calling
def make_boolean_array(values, mask=None):
import numpy as np
values = np.asarray(values, dtype=bool)
if mask is None:
mask = np.zeros(values.shape, dtype=bool)
else:
mask = np.asarray(mask, dtype=bool)
from pandas.core.arrays.boolean import BooleanArray
return BooleanArray(values, mask) Type guard
def is_bool_ndarray(x) -> bool:
import numpy as np
return isinstance(x, np.ndarray) and x.dtype == np.bool_ Try / catch
try:
from pandas.arrays import BooleanArray
ba = BooleanArray(values, mask)
except TypeError as e:
if "pd.array" in str(e):
ba = pd.array(values, dtype="boolean")
else:
raise Prevention
- 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
When it happens
Trigger: Directly instantiating pd.arrays.BooleanArray([True, False], mask) with a Python list or a non-bool ndarray instead of using pd.array(...).
Common situations: Copy-pasted examples that call BooleanArray(...) directly; library code that tried to skip the public constructor; misunderstandings of the public API surface.
Related errors
- cannot pass mask for BooleanArray input
- Need to pass bool-like values
- values should be {descr} numpy array. Use the 'pd.array' fun
- Array with ndim > 2 is not supported.
- Unsupported type '{type(values)}' for ArrowExtensionArray
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/9976541a40e8865f.
Report an issue: GitHub.