pandas-dev/pandas · error · TypeError
Cannot construct a ' ' from
Error message
Cannot construct a '{cls.__name__}' from '{string}' What it means
Thrown by StringDtype.construct_from_string in pandas/core/arrays/string_.py:309 when the input string does not match any of the recognized spellings ('string', 'str' under future infer_string, 'string[python]', 'string[pyarrow]'). The fall-through else branch raises TypeError listing the offending string so the dtype registry can treat it as 'not a StringDtype name'.
Solutions
- Use a supported spelling: 'string', 'string[python]', or 'string[pyarrow]'.
- If you meant plain Python strings, use dtype='object' or enable pd.options.future.infer_string = True then use 'str'.
- Double-check the spelling against the docstring table in StringDtype.construct_from_string.
Example fix
// before
pd.StringDtype.construct_from_string('string[arrow]') # typo, raises
// after
pd.StringDtype.construct_from_string('string[pyarrow]') Defensive patterns
Strategy: validation
Validate before calling
VALID = {'string', 'string[python]', 'string[pyarrow]'}
def safe_construct(name):
if name not in VALID:
raise ValueError(f'{name!r} not in {VALID}')
return pd.StringDtype.construct_from_string(name) Type guard
null
Try / catch
try:
return pd.StringDtype.construct_from_string(name)
except TypeError:
return None # fall back to object dtype Prevention
- Cross-check dtype strings against the docstring table of construct_from_string.
- For plain Python strings use 'string[python]' or enable infer_string and use 'str'.
- Wrap user-supplied dtype names in a whitelist at the config boundary.
When it happens
Trigger: Passing 'string[numpy]', 'object', 'category', 'str' (when infer_string is not enabled), 'string[arrow]' (typo), or any unrecognized dtype name to construct_from_string. Indirectly triggered by pd.api.types.pandas_dtype('string[foo]').
Common situations: Typo in a dtype string in a config file or constructor call. Using 'str' as a dtype name without enabling the future infer_string behavior. Third-party code passing a stale spelling removed in a pandas upgrade.
Related errors
- 'construct_from_string' expects a string, got
- dtype ' ' does not support operation
- dtype ' ' does not support operation
- dtype ' ' does not support operation 'quantile
- ' ' is not a valid function for ' ' object
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/f38fa69fe2a80240.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/string_.py:309
Raise
-----
TypeError
If the string is not a valid option.
"""
if not isinstance(string, str):
raise TypeError(
f"'construct_from_string' expects a string, got {type(string)}"
)
if string == "string":
return cls()
elif string == "str" and using_string_dtype():
return cls(na_value=np.nan)
elif string == "string[python]":
return cls(storage="python")
elif string == "string[pyarrow]":
return cls(storage="pyarrow")
else:
raise TypeError(f"Cannot construct a '{cls.__name__}' from '{string}'")
def construct_array_type(self) -> type_t[BaseStringArray]:
"""
Return the array type associated with this dtype.
Returns
-------
type
"""
from pandas.core.arrays.string_arrow import (
ArrowStringArray,
)
if self.storage == "python" and self._na_value is libmissing.NA:
return StringArray
elif self.storage == "pyarrow" and self._na_value is libmissing.NA:
return ArrowStringArray
elif self.storage == "python":View on GitHub (pinned to 3b7651241d)