pandas-dev/pandas · error · ValueError
cannot pass mask for BooleanArray input
Error message
cannot pass mask for BooleanArray input
What it means
Raised by coerce_to_array when values is already a BooleanArray and a separate mask argument is also passed. A BooleanArray already carries its own mask, so supplying another is ambiguous; pandas refuses to silently merge them.
Solutions
- Drop the mask argument when passing a BooleanArray; rely on its built-in mask.
- If you need a different mask, extract the underlying data first: boolean_array._data, then pass mask explicitly.
- Combine masks yourself: combined = boolean_array._mask | your_mask, then build BooleanArray(boolean_array._data, combined).
Example fix
// before pd.array(existing_bool_array, mask=my_mask, dtype='boolean') // after pd.array(existing_bool_array, dtype='boolean') // or BooleanArray(existing_bool_array._data, existing_bool_array._mask | my_mask)
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(values, BooleanArray) and mask is not None:
raise ValueError('BooleanArray already has a mask; drop the mask argument') Type guard
def is_boolean_array(v) -> bool:
from pandas.core.arrays.boolean import BooleanArray
return isinstance(v, BooleanArray) Try / catch
try:
coerce_to_array(values, mask=mask)
except ValueError as e:
if 'cannot pass mask' in str(e):
values, mask = values._data, values._mask
... Prevention
- Do not pass mask when values is already a BooleanArray.
- Extract _data first if you need a custom mask.
When it happens
Trigger: Calling pd.array(boolean_array, mask=..., dtype='boolean'), or BooleanArray(values=existing_boolean_array, mask=...). Also reachable via internal coerce_to_array calls when a BooleanArray is forwarded together with a user-supplied mask.
Common situations: Wrapping an existing nullable BooleanArray with an externally computed mask; pipeline code that always passes a mask regardless of input type; refactor that passes BooleanArray where previously an ndarray was used.
Related errors
- values.shape and mask.shape must match
- Need to pass bool-like values
- values should be boolean numpy array. Use the 'pd.array'…
- Cannot cast NaN value to Integer dtype.
- cannot convert float NaN to bool
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/d4c27a2a2202b24a.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/boolean.py:201
values, mask=None, copy: bool = False
) -> tuple[np.ndarray, np.ndarray]:
"""
Coerce the input values array to numpy arrays with a mask.
Parameters
----------
values : 1D list-like
mask : bool 1D array, optional
copy : bool, default False
if True, copy the input
Returns
-------
tuple of (values, mask)
"""
if isinstance(values, BooleanArray):
if mask is not None:
raise ValueError("cannot pass mask for BooleanArray input")
values, mask = values._data, values._mask
if copy:
values = values.copy()
mask = mask.copy()
return values, mask
mask_values = None
if isinstance(values, np.ndarray) and values.dtype == np.bool_:
if copy:
values = values.copy()
elif isinstance(values, np.ndarray) and values.dtype.kind in "iufcb":
mask_values = isna(values)
values_bool = np.zeros(len(values), dtype=bool)
values_bool[~mask_values] = values[~mask_values].astype(bool)
if not np.all(
values_bool[~mask_values].astype(values.dtype) == values[~mask_values]View on GitHub (pinned to 3b7651241d)