pandas-dev/pandas · error · ValueError

cannot convert to '{dtype}'-dtype NumPy array with missing v

Error message

cannot convert to '{dtype}'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.

What it means

Raised by BaseMaskedArray.to_numpy when the masked array contains missing values but the requested dtype is neither object nor string and no explicit na_value was given (defaults to pandas.NA). pandas cannot embed a pd.NA sentinel into a numeric/datetime numpy array, so it refuses rather than silently corrupting the result.

Source

Thrown at pandas/core/arrays/masked.py:709

        ValueError: cannot convert to bool numpy array in presence of missing values

        Specify a valid `na_value` instead

        >>> a.to_numpy(dtype="bool", na_value=False)
        array([ True, False, False])
        """
        hasna = self._hasna
        dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, hasna)
        if dtype is None:
            dtype = np.dtype(object)

        if hasna:
            if (
                dtype != np.dtype(object)
                and not is_string_dtype(dtype)
                and na_value is libmissing.NA
            ):
                raise ValueError(
                    f"cannot convert to '{dtype}'-dtype NumPy array "
                    "with missing values. Specify an appropriate 'na_value' "
                    "for this dtype."
                )
            # don't pass copy to astype -> always need a copy since we are mutating
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=RuntimeWarning)
                data = self._data.astype(dtype)
            data[self._mask] = na_value
        else:
            with warnings.catch_warnings():
                warnings.filterwarnings("ignore", category=RuntimeWarning)
                data = self._data.astype(dtype, copy=copy)
            if self._readonly and not copy and astype_is_view(self.dtype, dtype):
                data = data.view()
                data.flags.writeable = False
        return data

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Pass an explicit na_value compatible with the target dtype: arr.to_numpy(dtype='int64', na_value=-1) or arr.to_numpy(dtype='bool', na_value=False).
  2. Drop or fill missing values first: arr = arr[~arr.isna()] or use Series.fillna(...) before converting.
  3. Omit the dtype to get an object array that preserves pd.NA: arr.to_numpy().
  4. Cast to float64 if NaN semantics are acceptable: arr.to_numpy(dtype='float64', na_value=np.nan).

Example fix

// before
arr.to_numpy(dtype="int64")  # raises if arr has NA

// after
arr.to_numpy(dtype="int64", na_value=-1)
Defensive patterns

Strategy: validation

Validate before calling

def to_numpy_safe(arr, dtype=None):
    if getattr(arr, "_hasna", False) and dtype is not None:
        import numpy as np
        if np.dtype(dtype).kind in "iubM":
            # need an explicit na_value
            raise ValueError(f"pass na_value for dtype {dtype} with NAs present")
    return arr.to_numpy(dtype=dtype)

Type guard

def needs_na_value(arr, dtype) -> bool:
    import numpy as np
    return (getattr(arr, "_hasna", False)
            and np.dtype(dtype).kind not in "OUS")

Try / catch

try:
    out = arr.to_numpy(dtype="int64")
except ValueError:
    out = arr.to_numpy(dtype="int64", na_value=-1)

Prevention

When it happens

Trigger: Calling arr.to_numpy(dtype='int64'), arr.to_numpy(dtype='bool'), arr.to_numpy(dtype='datetime64[ns]') (or np.asarray(arr, dtype=...)) on a masked array where self._hasna is True, without passing a compatible na_value.

Common situations: Passing nullable ExtensionArray data into a numpy-only routine that demands a concrete numeric dtype; forgetting a column has NaNs; refactoring code that previously used float64 (which silently gets np.nan) to use integer dtypes.

Related errors


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