pandas-dev/pandas · error · TypeError
unsupported type: {into}
Error message
unsupported type: {into} What it means
Raised by standardize_mapping (pandas/core/common.py:428) used by Series.to_dict / DataFrame.to_dict when the `into` argument is not a subclass of collections.abc.Mapping. to_dict needs a dict-like constructor to build the result, so plain types like list, set, tuple, or arbitrary classes are rejected.
Source
Thrown at pandas/core/common.py:428
or an instance of a collections.abc.Mapping subclass.
Returns
-------
mapping : a collections.abc.Mapping subclass or other constructor
a callable object that can accept an iterator to create
the desired Mapping.
See Also
--------
DataFrame.to_dict
Series.to_dict
"""
if not inspect.isclass(into):
if isinstance(into, defaultdict):
return partial(defaultdict, into.default_factory)
into = type(into)
if not issubclass(into, abc.Mapping):
raise TypeError(f"unsupported type: {into}")
if into == defaultdict:
raise TypeError("to_dict() only accepts initialized defaultdicts")
return into
@overload
def random_state(state: np.random.Generator) -> np.random.Generator: ...
@overload
def random_state(
state: int | np.ndarray | np.random.BitGenerator | np.random.RandomState | None,
) -> np.random.RandomState: ...
def random_state(
state: RandomState | None = None,
) -> np.random.RandomState | np.random.Generator:View on GitHub (pinned to 71959b8cb9)
Solutions
- Pass a Mapping subclass: `into=dict` (default), `into=collections.OrderedDict`, or `into=collections.defaultdict(list)` (an instance).
- If you need a non-dict shape, call to_dict with the right orient and convert afterward, e.g. `df.to_dict('records')` returns a list of dicts.
- For a custom Mapping, ensure it subclasses collections.abc.Mapping and implements __getitem__, __iter__, __len__.
Example fix
# before
df.to_dict(into=list)
# after
list(df.to_dict('index').items()) Defensive patterns
Strategy: validation
Validate before calling
import collections.abc
def validate_into(into):
cls = into if isinstance(into, type) else type(into)
if not issubclass(cls, collections.abc.Mapping):
raise TypeError(f'into must be a Mapping subclass, got {cls}')
return into Type guard
import collections.abc
def is_mapping_type(into) -> bool:
cls = into if isinstance(into, type) else type(into)
return issubclass(cls, collections.abc.Mapping) Try / catch
try:
result = df.to_dict(into=into)
except TypeError as e:
if 'unsupported type' in str(e):
result = df.to_dict(into=dict)
else:
raise Prevention
- Only pass Mapping subclasses (dict, OrderedDict) or initialized defaultdict instances as into.
- For non-dict outputs, choose the right orient and convert afterward.
- Subclass collections.abc.Mapping for custom dict-likes.
When it happens
Trigger: `df.to_dict('records', into=list)`, `s.to_dict(into=set)`, `df.to_dict(into=tuple)`, or `into=SomeCustomClass` that does not subclass Mapping.
Common situations: Misunderstanding `into` as the output container type rather than a Mapping subclass. Passing a custom class that forgot to inherit from abc.Mapping.
Related errors
- to_dict() only accepts initialized defaultdicts
- Expected Hashable, got: {type(col_name)}
- Resolver of type '{name}' does not implement the __getitem__
- No such keys(s): {pat!r}
- {k} is not a valid identifier
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/b1a0be130287939d.
Report an issue: GitHub.