pandas-dev/pandas · error · ValueError

cannot convert to ' '-dtype NumPy array with missing…

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 array contains missing values but the requested dtype is neither object nor string and no usable na_value was supplied (defaults to libmissing.NA). pandas cannot represent masked NA inside a concrete numeric ndarray without an explicit sentinel, so it refuses rather than silently corrupting values.

Solutions

  1. Provide an explicit na_value: arr.to_numpy(dtype='float64', na_value=np.nan).
  2. Drop or fill missing values before conversion: series.dropna().to_numpy(dtype=...).
  3. Convert to object dtype to preserve pd.NA: arr.to_numpy(dtype=object).

Example fix

// before
arr = pd.array([1, None, 3], dtype='Int64')
arr.to_numpy(dtype='float64')   # raises
// after
arr.to_numpy(dtype='float64', na_value=np.nan)
Defensive patterns

Strategy: validation

Validate before calling

if arr._hasna and target_dtype not in (None, np.dtype(object)) and not is_string_dtype(target_dtype):
    out = arr.to_numpy(dtype=target_dtype, na_value=_sentinel_for(target_dtype))
else:
    out = arr.to_numpy(dtype=target_dtype)

Type guard

def needs_na_value(arr, dtype) -> bool:
    import numpy as np
    from pandas.api.types import is_string_dtype
    return arr._hasna and dtype not in (None, np.dtype(object)) and not is_string_dtype(dtype)

Try / catch

try:
    out = arr.to_numpy(dtype=dtype)
except ValueError as e:
    if 'cannot convert to' in str(e) and 'na_value' in str(e):
        out = arr.to_numpy(dtype=dtype, na_value=np.nan if dtype.kind == 'f' else -1)
    else:
        raise

Prevention

When it happens

Trigger: Calling arr.to_numpy(dtype='int64') or np.asarray(series, dtype='float64') on a masked array with hasna=True and default na_value; also triggered by __array__ paths that forward a non-object dtype.

Common situations: Converting a nullable Int64/Float64/boolean Series to a numpy array for a library that does not understand pandas NA; passing dtype= to to_numpy without considering NAs; interop with scikit-learn / scipy that require concrete dtypes.

Related errors


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

Appendix: 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 3b7651241d)