{"record":{"id":"7c0382811fd14c45","repo":"pandas-dev/pandas","slug":"stringarray-requires-a-sequence-of-strings-or-pand","errorCode":null,"errorMessage":"StringArray requires a sequence of strings or pandas.NA","messagePattern":"StringArray requires a sequence of strings or pandas\\.NA","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":723,"sourceCode":"        values = extract_array(values)\n\n        super().__init__(values, copy=copy)\n        if not isinstance(values, type(self)):\n            self._validate(dtype)\n        NDArrayBacked.__init__(\n            self,\n            self._ndarray,\n            dtype,\n        )\n\n    def _validate(self, dtype: StringDtype) -> None:\n        \"\"\"Validate that we only store NA or strings.\"\"\"\n\n        if dtype._na_value is libmissing.NA:\n            if len(self._ndarray) and not lib.is_string_array(\n                self._ndarray, skipna=True\n            ):\n                raise ValueError(\n                    \"StringArray requires a sequence of strings or pandas.NA\"\n                )\n            if self._ndarray.dtype != \"object\":\n                raise ValueError(\n                    \"StringArray requires a sequence of strings or pandas.NA. Got \"\n                    f\"'{self._ndarray.dtype}' dtype instead.\"\n                )\n            # Check to see if need to convert Na values to pd.NA\n            if self._ndarray.ndim > 2:\n                # Ravel if ndims > 2 b/c no cythonized version available\n                lib.convert_nans_to_NA(self._ndarray.ravel(\"K\"))\n            else:\n                lib.convert_nans_to_NA(self._ndarray)\n        else:\n            # Validate that we only store NaN or strings.\n            if len(self._ndarray) and not lib.is_string_array(\n                self._ndarray, skipna=True\n            ):","sourceCodeStart":705,"sourceCodeEnd":741,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L705-L741","documentation":"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.","triggerScenarios":"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).","commonSituations":"Building a StringArray by hand instead of via pd.array(..., dtype='string'); feeding mixed-type object arrays that were not pre-cleaned.","solutions":["Use pd.array(values, dtype='string') which coerces non-strings to str.","Pre-convert all values to str (and use pd.NA/None/np.nan for missing) before constructing.","Ensure the input object array contains only str and NA-like values."],"exampleFix":"// before\narr = pd.arrays.StringArray(np.array([1, 'a', None], dtype=object))\n\n// after\narr = pd.array([1, 'a', None], dtype='string')","handlingStrategy":"validation","validationCode":"clean = [str(v) if not (v is None or v is pd.NA) else v for v in values]\narr = pd.array(clean, dtype='string')","typeGuard":"import pandas as pd\n\ndef all_strings_or_na(values) -> bool:\n    return all(isinstance(v, str) or pd.isna(v) for v in values)","tryCatchPattern":null,"preventionTips":["Prefer pd.array(values, dtype='string') over direct StringArray construction.","Coerce non-string values to str before building the array.","Keep only str and NA-like values (None, pd.NA, np.nan) in the input."],"tags":["string-array","validation","construction"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}