pandas-dev/pandas · error · TypeError
'construct_from_string' expects a string, got
Error message
'construct_from_string' expects a string, got {type(string)} What it means
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.
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.
Example fix
// before
pd.StringDtype.construct_from_string(np.dtype('O')) # raises TypeError
// after
pd.StringDtype.construct_from_string('string[python]') Defensive patterns
Strategy: type-guard
Validate before calling
def safe_construct(name):
if not isinstance(name, str):
raise TypeError(f'expected str, got {type(name).__name__}')
return pd.StringDtype.construct_from_string(name) Type guard
def is_dtype_name_str(x) -> bool:
return isinstance(x, str) Try / catch
null
Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Cannot construct a ' ' from
- dtype ' ' does not support operation
- dtype ' ' does not support operation
- dtype ' ' does not support operation 'quantile
- Expected coo_matrix. Got
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/393400bf083f5e50.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/string_.py:297
========================== ==============================================
string result storage
========================== ==============================================
``'string'`` pd.options.mode.string_storage, default python
``'string[python]'`` python
``'string[pyarrow]'`` pyarrow
========================== ==============================================
Returns
-------
StringDtype
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.
ReturnsView on GitHub (pinned to 3b7651241d)