pandas-dev/pandas · error · TypeError
unsupported type
Error message
unsupported type: {into} What it means
Raised by the to_dict helper (converted_to_mapping) when the `into` argument is not a class or instance of a collections.abc.Mapping subclass. The helper first normalizes instances to their type, then checks issubclass(into, abc.Mapping); any non-Mapping type (list, set, tuple, a custom class without Mapping) triggers TypeError with the offending type rendered.
Solutions
- Pass a Mapping subclass: dict (default), collections.OrderedDict, collections.defaultdict instance.
- For list output, use ser.tolist() or df.to_dict('list').
- If using a custom class, subclass collections.abc.Mapping first.
Example fix
// before out = ser.to_dict(into=list) // after out = ser.tolist() # or ser.to_dict(into=collections.OrderedDict)
Defensive patterns
Strategy: validation
Validate before calling
import collections.abc
if not (inspect.isclass(into) and issubclass(into, collections.abc.Mapping)):
raise TypeError(f'{into} is not a Mapping subclass')
out = ser.to_dict(into=into) Type guard
def is_mapping_factory(into) -> bool:
import collections.abc, inspect
cls = into if inspect.isclass(into) else type(into)
return issubclass(cls, collections.abc.Mapping) Prevention
- Pass dict or another Mapping subclass as `into`.
- Use ser.tolist() for list output, not to_dict(into=list).
When it happens
Trigger: ser.to_dict(into=list); df.to_dict(into=set); ser.to_dict(into=collections.OrderedDict) is fine (Mapping subclass); ser.to_dict(into=tuple) fails.
Common situations: Confusing to_dict 'into' (a mapping factory) with tolist()/records; passing a custom dataclass or namedtuple thinking it acts as a dict; old tutorials suggesting list.
Related errors
- to_dict() only accepts initialized defaultdicts
- Expected Hashable, got
- to_numpy() got an unexpected keyword argument
- Accumulation not supported for
- boolean value of an expression is ambiguous
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/b1a0be130287939d.
Report an issue: GitHub.
Appendix: 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 3b7651241d)