{"record":{"id":"b1a0be130287939d","repo":"pandas-dev/pandas","slug":"unsupported-type-into","errorCode":null,"errorMessage":"unsupported type: {into}","messagePattern":"unsupported type: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/common.py","lineNumber":428,"sourceCode":"        or an instance of a collections.abc.Mapping subclass.\n\n    Returns\n    -------\n    mapping : a collections.abc.Mapping subclass or other constructor\n        a callable object that can accept an iterator to create\n        the desired Mapping.\n\n    See Also\n    --------\n    DataFrame.to_dict\n    Series.to_dict\n    \"\"\"\n    if not inspect.isclass(into):\n        if isinstance(into, defaultdict):\n            return partial(defaultdict, into.default_factory)\n        into = type(into)\n    if not issubclass(into, abc.Mapping):\n        raise TypeError(f\"unsupported type: {into}\")\n    if into == defaultdict:\n        raise TypeError(\"to_dict() only accepts initialized defaultdicts\")\n    return into\n\n\n@overload\ndef random_state(state: np.random.Generator) -> np.random.Generator: ...\n\n\n@overload\ndef random_state(\n    state: int | np.ndarray | np.random.BitGenerator | np.random.RandomState | None,\n) -> np.random.RandomState: ...\n\n\ndef random_state(\n    state: RandomState | None = None,\n) -> np.random.RandomState | np.random.Generator:","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/common.py#L410-L446","documentation":"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.","triggerScenarios":"`df.to_dict('records', into=list)`, `s.to_dict(into=set)`, `df.to_dict(into=tuple)`, or `into=SomeCustomClass` that does not subclass Mapping.","commonSituations":"Misunderstanding `into` as the output container type rather than a Mapping subclass. Passing a custom class that forgot to inherit from abc.Mapping.","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__."],"exampleFix":"# before\ndf.to_dict(into=list)\n\n# after\nlist(df.to_dict('index').items())","handlingStrategy":"validation","validationCode":"import collections.abc\n\ndef validate_into(into):\n    cls = into if isinstance(into, type) else type(into)\n    if not issubclass(cls, collections.abc.Mapping):\n        raise TypeError(f'into must be a Mapping subclass, got {cls}')\n    return into","typeGuard":"import collections.abc\n\ndef is_mapping_type(into) -> bool:\n    cls = into if isinstance(into, type) else type(into)\n    return issubclass(cls, collections.abc.Mapping)","tryCatchPattern":"try:\n    result = df.to_dict(into=into)\nexcept TypeError as e:\n    if 'unsupported type' in str(e):\n        result = df.to_dict(into=dict)\n    else:\n        raise","preventionTips":["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."],"tags":["to-dict","mapping","into","validation","typeerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}