pandas-dev/pandas · error · TypeError

'construct_from_string' expects a string, got {type(string)}

Error message

'construct_from_string' expects a string, got {type(string)}

What it means

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.

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.

        Returns

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Ensure the argument is a str before calling: construct_from_string(str(x)).
  2. Pass the dtype object directly to APIs that accept ExtensionDtype instead of routing through construct_from_string.
  3. Validate input type at the boundary of your own code.

Example fix

// before
StringDtype.construct_from_string(dtype_obj)

// after
StringDtype.construct_from_string(str(dtype_name))
Defensive patterns

Strategy: type-guard

Validate before calling

name = str(name) if not isinstance(name, str) else name
dtype = pd.StringDtype.construct_from_string(name)

Type guard

def is_dtype_name(x) -> bool:
    return isinstance(x, str)

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/393400bf083f5e50. Report an issue: GitHub.