{"record":{"id":"f38fa69fe2a80240","repo":"pandas-dev/pandas","slug":"cannot-construct-a-cls-name-from-string","errorCode":null,"errorMessage":"Cannot construct a '{cls.__name__}' from '{string}'","messagePattern":"Cannot construct a '(.+?)' from '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":309,"sourceCode":"        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\n        -------\n        type\n        \"\"\"\n        from pandas.core.arrays.string_arrow import (\n            ArrowStringArray,\n        )\n\n        if self.storage == \"python\" and self._na_value is libmissing.NA:\n            return StringArray\n        elif self.storage == \"pyarrow\" and self._na_value is libmissing.NA:\n            return ArrowStringArray\n        elif self.storage == \"python\":","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L291-L327","documentation":"construct_from_string accepts only the literal names 'string', 'str' (when the experimental string_dtype config is enabled), 'string[python]', and 'string[pyarrow]'. Any other string falls through to the else branch and raises TypeError indicating the name cannot be resolved to a StringDtype.","triggerScenarios":"Calling construct_from_string('string[foo]'), construct_from_string('object'), construct_from_string('int64'), construct_from_string('StringDtype'), or any unrecognized dtype string.","commonSituations":"Typos in dtype strings; passing a dtype name belonging to a different dtype; using a storage suffix that does not exist (e.g., 'string[arrow]' instead of 'string[pyarrow]').","solutions":["Use one of the valid names: 'string', 'string[python]', 'string[pyarrow]' (or 'str' under the experimental config).","Double-check spelling, especially the 'pyarrow' suffix.","If you need a different dtype, use its own construct_from_string or pandas_dtype()."],"exampleFix":"// before\nStringDtype.construct_from_string('string[arrow]')\n\n// after\nStringDtype.construct_from_string('string[pyarrow]')","handlingStrategy":"validation","validationCode":"VALID = {'string', 'string[python]', 'string[pyarrow]'}\nif name not in VALID:\n    raise ValueError(f'Invalid string dtype name: {name}')\ndtype = pd.StringDtype.construct_from_string(name)","typeGuard":"VALID = {'string', 'string[python]', 'string[pyarrow]'}\n\ndef is_valid_string_dtype_name(s: str) -> bool:\n    return isinstance(s, str) and s in VALID","tryCatchPattern":"try:\n    dtype = pd.StringDtype.construct_from_string(name)\nexcept TypeError:\n    dtype = pd.StringDtype()","preventionTips":["Whitelist the valid dtype-name strings and validate against it.","Watch for the 'pyarrow' suffix spelling.","Use pandas_dtype() for general dtype resolution from arbitrary names."],"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"}