pandas-dev/pandas · error · ValueError

StringArray requires a sequence of strings or pandas.NA

Error message

StringArray requires a sequence of strings or pandas.NA

What it means

StringArray._validate runs when constructing a StringArray whose dtype uses pandas.NA semantics. It calls lib.is_string_array(ndarray, skipna=True); if any non-NA element is not a Python str, it raises ValueError. This is the content check that runs before the dtype-kind check.

Source

Thrown at pandas/core/arrays/string_.py:723

        values = extract_array(values)

        super().__init__(values, copy=copy)
        if not isinstance(values, type(self)):
            self._validate(dtype)
        NDArrayBacked.__init__(
            self,
            self._ndarray,
            dtype,
        )

    def _validate(self, dtype: StringDtype) -> None:
        """Validate that we only store NA or strings."""

        if dtype._na_value is libmissing.NA:
            if len(self._ndarray) and not lib.is_string_array(
                self._ndarray, skipna=True
            ):
                raise ValueError(
                    "StringArray requires a sequence of strings or pandas.NA"
                )
            if self._ndarray.dtype != "object":
                raise ValueError(
                    "StringArray requires a sequence of strings or pandas.NA. Got "
                    f"'{self._ndarray.dtype}' dtype instead."
                )
            # Check to see if need to convert Na values to pd.NA
            if self._ndarray.ndim > 2:
                # Ravel if ndims > 2 b/c no cythonized version available
                lib.convert_nans_to_NA(self._ndarray.ravel("K"))
            else:
                lib.convert_nans_to_NA(self._ndarray)
        else:
            # Validate that we only store NaN or strings.
            if len(self._ndarray) and not lib.is_string_array(
                self._ndarray, skipna=True
            ):

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Use pd.array(values, dtype='string') which coerces non-strings to str.
  2. Pre-convert all values to str (and use pd.NA/None/np.nan for missing) before constructing.
  3. Ensure the input object array contains only str and NA-like values.

Example fix

// before
arr = pd.arrays.StringArray(np.array([1, 'a', None], dtype=object))

// after
arr = pd.array([1, 'a', None], dtype='string')
Defensive patterns

Strategy: validation

Validate before calling

clean = [str(v) if not (v is None or v is pd.NA) else v for v in values]
arr = pd.array(clean, dtype='string')

Type guard

import pandas as pd

def all_strings_or_na(values) -> bool:
    return all(isinstance(v, str) or pd.isna(v) for v in values)

Prevention

When it happens

Trigger: Constructing StringArray(np.array([1, 'a'], dtype=object)) directly, or StringArray(np.array([1.5, None], dtype=object)), with the default NA-semantics dtype. Also reached via internal paths that bypass _from_sequence (which coerces).

Common situations: Building a StringArray by hand instead of via pd.array(..., dtype='string'); feeding mixed-type object arrays that were not pre-cleaned.

Related errors


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