{"record":{"id":"393400bf083f5e50","repo":"pandas-dev/pandas","slug":"construct-from-string-expects-a-string-got-typ","errorCode":null,"errorMessage":"'construct_from_string' expects a string, got {type(string)}","messagePattern":"'construct_from_string' expects a string, got (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":297,"sourceCode":"            ========================== ==============================================\n            string                     result storage\n            ========================== ==============================================\n            ``'string'``               pd.options.mode.string_storage, default python\n            ``'string[python]'``       python\n            ``'string[pyarrow]'``      pyarrow\n            ========================== ==============================================\n\n        Returns\n        -------\n        StringDtype\n\n        Raise\n        -----\n        TypeError\n            If the string is not a valid option.\n        \"\"\"\n        if not isinstance(string, str):\n            raise TypeError(\n                f\"'construct_from_string' expects a string, got {type(string)}\"\n            )\n        if string == \"string\":\n            return cls()\n        elif string == \"str\" and using_string_dtype():\n            return cls(na_value=np.nan)\n        elif string == \"string[python]\":\n            return cls(storage=\"python\")\n        elif string == \"string[pyarrow]\":\n            return cls(storage=\"pyarrow\")\n        else:\n            raise TypeError(f\"Cannot construct a '{cls.__name__}' from '{string}'\")\n\n    def construct_array_type(self) -> type_t[BaseStringArray]:\n        \"\"\"\n        Return the array type associated with this dtype.\n\n        Returns","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_.py#L279-L315","documentation":"Thrown by StringDtype.construct_from_string in pandas/core/arrays/string_.py:297 when the `string` argument is not a Python str. The classmethod is part of the ExtensionDtype registry contract and expects a dtype-name string; passing a non-string triggers TypeError before the dispatch table is consulted.","triggerScenarios":"Internal pandas dtype machinery calls construct_from_string with a non-string object (e.g., a numpy.dtype, an int, None) during dtype inference. Direct user calls like pd.StringDtype.construct_from_string(np.dtype('O')) or construct_from_string(None).","commonSituations":"A custom serializer or third-party library invokes the dtype registry with unvalidated input. Programmatic dtype construction where the caller forgot to stringify the value. None passed where a dtype name was expected.","solutions":["Pass a string literal: 'string', 'string[python]', or 'string[pyarrow]'.","Stringify before calling: construct_from_string(str(name)) only if the str form is a valid StringDtype name.","If you have a dtype object, compare with isinstance(dtype, StringDtype) rather than reconstructing from a string."],"exampleFix":"// before\npd.StringDtype.construct_from_string(np.dtype('O'))  # raises TypeError\n\n// after\npd.StringDtype.construct_from_string('string[python]')","handlingStrategy":"type-guard","validationCode":"def safe_construct(name):\n    if not isinstance(name, str):\n        raise TypeError(f'expected str, got {type(name).__name__}')\n    return pd.StringDtype.construct_from_string(name)","typeGuard":"def is_dtype_name_str(x) -> bool:\n    return isinstance(x, str)","tryCatchPattern":"null","preventionTips":["Always stringify dtype names before calling construct_from_string.","Prefer pd.api.types.pandas_dtype(name) which handles the registry dispatch.","Do not pass numpy.dtype objects or dtype instances to construct_from_string."],"tags":["string-dtype","dtype-registry","type-check"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}