pandas-dev/pandas · error · TypeError
to_numpy() got an unexpected keyword argument
Error message
to_numpy() got an unexpected keyword argument '{bad_keys}' What it means
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.
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')).
Example fix
// before arr = ser.to_numpy(dtype='int64', na_vlaue=0) // after arr = ser.to_numpy(dtype='int64', na_value=0)
Defensive patterns
Strategy: validation
Validate before calling
import inspect
allowed = {'dtype', 'copy', 'na_value'}
extra = set(kwargs) - allowed
if extra and not isinstance(ser.dtype, pd.api.types.ExtensionDtype):
raise TypeError(f'unsupported kwargs: {extra}')
arr = ser.to_numpy(**kwargs) Prevention
- Spell parameters exactly as the to_numpy signature (dtype, copy, na_value).
- Be aware ExtensionDtype-backed Series accept more kwargs than NumPy-backed ones.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Expected Hashable, got
- is both the pipe target and a keyword argument
- to_dict() only accepts initialized defaultdicts
- unsupported type
- Accumulation not supported for
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/4f0e1f0e48b43dd6.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/base.py:700
>>> ser.to_numpy(dtype=object)
array([Timestamp('2000-01-01 00:00:00+0100', tz='CET'),
Timestamp('2000-01-02 00:00:00+0100', tz='CET')],
dtype=object)
Or ``dtype='datetime64[ns]'`` to return an ndarray of native
datetime64 values. The values are converted to UTC and the timezone
info is dropped.
>>> ser.to_numpy(dtype="datetime64[ns]")
... # doctest: +ELLIPSIS
array(['1999-12-31T23:00:00.000000000', '2000-01-01T23:00:00...'],
dtype='datetime64[ns]')
"""
if isinstance(self.dtype, ExtensionDtype):
return self.array.to_numpy(dtype, copy=copy, na_value=na_value, **kwargs)
elif kwargs:
bad_keys = next(iter(kwargs.keys()))
raise TypeError(
f"to_numpy() got an unexpected keyword argument '{bad_keys}'"
)
fillna = (
na_value is not lib.no_default
# no need to fillna with np.nan if we already have a float dtype
and not (na_value is np.nan and np.issubdtype(self.dtype, np.floating))
)
values = self._values
if fillna and self.hasnans:
if not can_hold_element(values, na_value):
# if we can't hold the na_value asarray either makes a copy or we
# error before modifying values. The asarray later on thus won't make
# another copy
values = np.asarray(values, dtype=dtype)
else:
values = values.copy()View on GitHub (pinned to 3b7651241d)