pandas-dev/pandas · error · ValueError

cannot pass mask for BooleanArray input

Error message

cannot pass mask for BooleanArray input

What it means

coerce_to_array (boolean.py:201) refuses a separately-passed mask when the input `values` is already a BooleanArray, because the BooleanArray carries its own mask and combining two masks would be ambiguous. It raises ValueError to prevent silent data corruption.

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 71959b8cb9)

Solutions

  1. Do not pass a mask when the input is already a BooleanArray; let its built-in mask be reused.
  2. If you need a custom mask, extract the underlying data first: ba._data and pass that with your mask.
  3. Use the high-level pd.array(values, dtype='boolean') constructor instead of low-level coerce_to_array.
  4. Validate the input type before forwarding to coerce_to_array.

Example fix

# before
ba = pd.array([True, False], dtype="boolean")
coerce_to_array(ba, mask=my_mask)  # raises

# after
coerce_to_array(ba._data, mask=my_mask)
Defensive patterns

Strategy: validation

Validate before calling

def safe_coerce(values, mask=None):
    from pandas.core.arrays.boolean import coerce_to_array, BooleanArray
    if isinstance(values, BooleanArray) and mask is not None:
        values = values._data
    return coerce_to_array(values, mask=mask)

Type guard

def is_boolean_array(x) -> bool:
    from pandas.core.arrays.boolean import BooleanArray
    return isinstance(x, BooleanArray)

Try / catch

try:
    v, m = coerce_to_array(values, mask=mask)
except ValueError as e:
    if "cannot pass mask for BooleanArray input" in str(e):
        v, m = coerce_to_array(values._data, mask=mask)
    else:
        raise

Prevention

When it happens

Trigger: Calling pd.array(BooleanArray_instance, mask=...) or BooleanArray._from_sequence with both a BooleanArray and a mask; internal callers of coerce_to_array that pass an explicit mask alongside an already-masked array.

Common situations: Library/extension code that double-masks data; refactoring that passed the wrong object into a boolean coercion helper; constructing BooleanArray via low-level APIs instead of pd.array.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/d4c27a2a2202b24a. Report an issue: GitHub.