{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/base.py#L682-L718","documentation":"Raised by IndexOpsMixin.to_numpy on non-ExtensionDtype Series/Index when extra **kwargs are present. ExtensionArrays receive kwargs and may accept them (e.g. na_value); for NumPy-backed data only dtype, copy, na_value are valid, so any leftover kwarg is rejected with a TypeError naming the first offending key. This guards against silent typos like na_vlaue=np.nan being dropped.","triggerScenarios":"Calling ser.to_numpy(dtype='int64', na_value=0, foo=1) on an int Series; misspelling na_value (na_vlaue, na_val); passing a kwarg valid only for EA dtypes (e.g. dtype='datetime64[ns]' on an ExtensionDtype that already routed into the EA branch is fine, but extra kwargs on a plain ndarray-backed Series are not).","commonSituations":"Typing na_value incorrectly; copy-pasting kwargs from an EA example (e.g. Arrow-backed) into code that runs on NumPy-backed Series; version differences where a kwarg was removed or never existed.","solutions":["Remove the offending kwarg; the message names it.","Check the spelled parameter name against to_numpy signature (dtype, copy, na_value).","If you need EA-specific kwargs, ensure the Series actually has an ExtensionDtype (e.g. convert with .astype('Int64'))."],"exampleFix":"// before\narr = ser.to_numpy(dtype='int64', na_vlaue=0)\n// after\narr = ser.to_numpy(dtype='int64', na_value=0)","handlingStrategy":"validation","validationCode":"import inspect\nallowed = {'dtype', 'copy', 'na_value'}\nextra = set(kwargs) - allowed\nif extra and not isinstance(ser.dtype, pd.api.types.ExtensionDtype):\n    raise TypeError(f'unsupported kwargs: {extra}')\narr = ser.to_numpy(**kwargs)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Spell parameters exactly as the to_numpy signature (dtype, copy, na_value).","Be aware ExtensionDtype-backed Series accept more kwargs than NumPy-backed ones."],"tags":["type-error","to-numpy","kwargs","series","api-misuse"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}