{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L279-L315","documentation":"StringDtype.construct_from_string is a classmethod that resolves a dtype name string into a StringDtype instance. It explicitly type-checks its argument: if `string` is not a str instance, it raises TypeError. This guards the internal dtype-resolution machinery which always passes strings.","triggerScenarios":"Calling StringDtype.construct_from_string(123), construct_from_string(None), construct_from_string(['string']), or any path that pipes a non-string object through dtype construction (e.g., a buggy registry or a dynamically typed caller).","commonSituations":"Programmatic dtype construction from untrusted/dynamic input where the value is not guaranteed to be a string; passing a dtype object instead of its name string.","solutions":["Ensure the argument is a str before calling: construct_from_string(str(x)).","Pass the dtype object directly to APIs that accept ExtensionDtype instead of routing through construct_from_string.","Validate input type at the boundary of your own code."],"exampleFix":"// before\nStringDtype.construct_from_string(dtype_obj)\n\n// after\nStringDtype.construct_from_string(str(dtype_name))","handlingStrategy":"type-guard","validationCode":"name = str(name) if not isinstance(name, str) else name\ndtype = pd.StringDtype.construct_from_string(name)","typeGuard":"def is_dtype_name(x) -> bool:\n    return isinstance(x, str)","tryCatchPattern":null,"preventionTips":["Ensure dtype-name inputs are str before calling construct_from_string.","Pass dtype objects directly to APIs that accept ExtensionDtype.","Validate dynamic input types at the boundary of your code."],"tags":["string-dtype","type-error","construct-from-string"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}