{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/common.py#L410-L446","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nout = ser.to_dict(into=list)\n// after\nout = ser.tolist()  # or ser.to_dict(into=collections.OrderedDict)","handlingStrategy":"validation","validationCode":"import collections.abc\nif not (inspect.isclass(into) and issubclass(into, collections.abc.Mapping)):\n    raise TypeError(f'{into} is not a Mapping subclass')\nout = ser.to_dict(into=into)","typeGuard":"def is_mapping_factory(into) -> bool:\n    import collections.abc, inspect\n    cls = into if inspect.isclass(into) else type(into)\n    return issubclass(cls, collections.abc.Mapping)","tryCatchPattern":null,"preventionTips":["Pass dict or another Mapping subclass as `into`.","Use ser.tolist() for list output, not to_dict(into=list)."],"tags":["type-error","to-dict","mapping","api-misuse"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}