{"record":{"id":"01071b5720b8e61a","repo":"pandas-dev/pandas","slug":"cannot-convert-to-dtype-dtype-numpy-array-with","errorCode":null,"errorMessage":"cannot convert to '{dtype}'-dtype NumPy array with missing values. Specify an appropriate 'na_value' for this dtype.","messagePattern":"cannot convert to '(.+?)'-dtype NumPy array with missing values\\. Specify an appropriate 'na_value' for this dtype\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/masked.py","lineNumber":709,"sourceCode":"        ValueError: cannot convert to bool numpy array in presence of missing values\n\n        Specify a valid `na_value` instead\n\n        >>> a.to_numpy(dtype=\"bool\", na_value=False)\n        array([ True, False, False])\n        \"\"\"\n        hasna = self._hasna\n        dtype, na_value = to_numpy_dtype_inference(self, dtype, na_value, hasna)\n        if dtype is None:\n            dtype = np.dtype(object)\n\n        if hasna:\n            if (\n                dtype != np.dtype(object)\n                and not is_string_dtype(dtype)\n                and na_value is libmissing.NA\n            ):\n                raise ValueError(\n                    f\"cannot convert to '{dtype}'-dtype NumPy array \"\n                    \"with missing values. Specify an appropriate 'na_value' \"\n                    \"for this dtype.\"\n                )\n            # don't pass copy to astype -> always need a copy since we are mutating\n            with warnings.catch_warnings():\n                warnings.filterwarnings(\"ignore\", category=RuntimeWarning)\n                data = self._data.astype(dtype)\n            data[self._mask] = na_value\n        else:\n            with warnings.catch_warnings():\n                warnings.filterwarnings(\"ignore\", category=RuntimeWarning)\n                data = self._data.astype(dtype, copy=copy)\n            if self._readonly and not copy and astype_is_view(self.dtype, dtype):\n                data = data.view()\n                data.flags.writeable = False\n        return data\n","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/masked.py#L691-L727","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide an explicit na_value: arr.to_numpy(dtype='float64', na_value=np.nan).","Drop or fill missing values before conversion: series.dropna().to_numpy(dtype=...).","Convert to object dtype to preserve pd.NA: arr.to_numpy(dtype=object)."],"exampleFix":"// before\narr = pd.array([1, None, 3], dtype='Int64')\narr.to_numpy(dtype='float64')   # raises\n// after\narr.to_numpy(dtype='float64', na_value=np.nan)","handlingStrategy":"validation","validationCode":"if arr._hasna and target_dtype not in (None, np.dtype(object)) and not is_string_dtype(target_dtype):\n    out = arr.to_numpy(dtype=target_dtype, na_value=_sentinel_for(target_dtype))\nelse:\n    out = arr.to_numpy(dtype=target_dtype)","typeGuard":"def needs_na_value(arr, dtype) -> bool:\n    import numpy as np\n    from pandas.api.types import is_string_dtype\n    return arr._hasna and dtype not in (None, np.dtype(object)) and not is_string_dtype(dtype)","tryCatchPattern":"try:\n    out = arr.to_numpy(dtype=dtype)\nexcept ValueError as e:\n    if 'cannot convert to' in str(e) and 'na_value' in str(e):\n        out = arr.to_numpy(dtype=dtype, na_value=np.nan if dtype.kind == 'f' else -1)\n    else:\n        raise","preventionTips":["Always pass na_value when converting nullable arrays to a concrete numeric dtype.","Drop or fill NAs upstream of any concrete-dtype numpy conversion.","Use object dtype when you need to preserve pd.NA through interop boundaries."],"tags":["pandas","masked-array","to-numpy","na-value","dtype"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}