pandas-dev/pandas · error · ValueError
StringArray requires a sequence of strings or pandas.NA. Got
Error message
StringArray requires a sequence of strings or pandas.NA. Got '{self._ndarray.dtype}' dtype instead. What it means
After the content check passes (or on an empty array), StringArray._validate (NA semantics) verifies the underlying ndarray has object dtype. StringArray stores Python str objects, so a typed ndarray (int64, float64, '<U...') is rejected with ValueError showing the offending dtype.
Source
Thrown at pandas/core/arrays/string_.py:727
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
):
raise ValueError("StringArray requires a sequence of strings or NaN")
if self._ndarray.dtype != "object":
raise ValueError(
"StringArray requires a sequence of strings "View on GitHub (pinned to 71959b8cb9)
Solutions
- Create the ndarray with dtype=object: np.array(values, dtype=object).
- Use pd.array(values, dtype='string') which handles dtype correctly.
- Convert '<U' arrays with .astype(object) before passing.
Example fix
// before arr = pd.arrays.StringArray(np.array(['a','b'])) // after arr = pd.arrays.StringArray(np.array(['a','b'], dtype=object))
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
values = np.asarray(values)
if values.dtype != object:
values = values.astype(object)
arr = pd.array(values, dtype='string') Type guard
import numpy as np
def is_object_ndarray(values) -> bool:
return isinstance(values, np.ndarray) and values.dtype == object Prevention
- Create ndarrays for StringArray with dtype=object.
- Use pd.array(values, dtype='string') to avoid dtype pitfalls.
- Cast '<U' numpy arrays via .astype(object) before construction.
When it happens
Trigger: Constructing StringArray(np.array([1, 2, 3])) (int64), StringArray(np.array([1.0, 2.0])) (float64), or StringArray(np.array(['a','b'])) which yields a '<U1' dtype rather than object.
Common situations: Passing numpy arrays created without dtype=object; assuming StringArray accepts unicode ('<U') ndarrays directly.
Related errors
- StringArray requires a sequence of strings or NaN. Got '{sel
- StringArray requires a sequence of strings or pandas.NA
- StringArray requires a sequence of strings or NaN
- to_concat must have the same dtype
- {dtype=} does not have a resolution.
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/3ec8e507a5945e5a.
Report an issue: GitHub.