{"record":{"id":"4f0e1f0e48b43dd6","repo":"pandas-dev/pandas","slug":"to-numpy-got-an-unexpected-keyword-argument-ba","errorCode":null,"errorMessage":"to_numpy() got an unexpected keyword argument '{bad_keys}'","messagePattern":"to_numpy\\(\\) got an unexpected keyword argument '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/base.py","lineNumber":700,"sourceCode":"        >>> ser.to_numpy(dtype=object)\n        array([Timestamp('2000-01-01 00:00:00+0100', tz='CET'),\n               Timestamp('2000-01-02 00:00:00+0100', tz='CET')],\n              dtype=object)\n\n        Or ``dtype='datetime64[ns]'`` to return an ndarray of native\n        datetime64 values. The values are converted to UTC and the timezone\n        info is dropped.\n\n        >>> ser.to_numpy(dtype=\"datetime64[ns]\")\n        ... # doctest: +ELLIPSIS\n        array(['1999-12-31T23:00:00.000000000', '2000-01-01T23:00:00...'],\n              dtype='datetime64[ns]')\n        \"\"\"\n        if isinstance(self.dtype, ExtensionDtype):\n            return self.array.to_numpy(dtype, copy=copy, na_value=na_value, **kwargs)\n        elif kwargs:\n            bad_keys = next(iter(kwargs.keys()))\n            raise TypeError(\n                f\"to_numpy() got an unexpected keyword argument '{bad_keys}'\"\n            )\n\n        fillna = (\n            na_value is not lib.no_default\n            # no need to fillna with np.nan if we already have a float dtype\n            and not (na_value is np.nan and np.issubdtype(self.dtype, np.floating))\n        )\n\n        values = self._values\n        if fillna and self.hasnans:\n            if not can_hold_element(values, na_value):\n                # if we can't hold the na_value asarray either makes a copy or we\n                # error before modifying values. The asarray later on thus won't make\n                # another copy\n                values = np.asarray(values, dtype=dtype)\n            else:\n                values = values.copy()","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/base.py#L682-L718","documentation":"Raised by Series/Index.to_numpy when extra **kwargs are passed but self.dtype is not an ExtensionDtype. For extension dtypes the kwargs forward to array.to_numpy; for numpy-backed dtypes those kwargs are unsupported and the first offending key is named.","triggerScenarios":"`numpy_backed_series.to_numpy(na_value=0)` or passing dtype/copy/na_value combinations on a plain int64/float64 Series that the numpy path does not accept.","commonSituations":"Generic helper code forwarding the same kwargs to all dtypes; assuming na_value is universally accepted; migrating from extension to numpy dtypes.","solutions":["Restrict kwargs to the documented set: dtype, copy, na_value are honored only when applicable.","Branch on isinstance(s.dtype, pd.ExtensionDtype) before forwarding kwargs.","Pre-fill NA values yourself via fillna before to_numpy."],"exampleFix":"// before\narr = s.to_numpy(na_value=-1)  # s is int64 numpy-backed\n\n// after\narr = s.fillna(-1).to_numpy()","handlingStrategy":"type-guard","validationCode":"from pandas.api.types import is_extension_array_dtype\nif not is_extension_array_dtype(s.dtype):\n    bad = set(kwargs) - {'dtype','copy'}\n    if bad:\n        raise TypeError(f'unsupported to_numpy kwargs for numpy dtype: {bad}')","typeGuard":"def accepts_to_numpy_kwargs(s) -> bool:\n    from pandas.api.types import is_extension_array_dtype\n    return is_extension_array_dtype(s.dtype)","tryCatchPattern":"try:\n    arr = s.to_numpy(**kwargs)\nexcept TypeError as e:\n    if 'unexpected keyword argument' in str(e):\n        arr = s.fillna(kwargs.get('na_value')).to_numpy(\n            dtype=kwargs.get('dtype'), copy=kwargs.get('copy', False))\n    else:\n        raise","preventionTips":["Only forward na_value to extension-dtype Series.","Branch kwargs on dtype kind.","Pre-fill NA values via fillna before to_numpy."],"tags":["to-numpy","typeerror","kwargs","dtype"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}