{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_.py#L705-L741","documentation":"Thrown by StringArray._validate in pandas/core/arrays/string_.py:723 during construction when na_value is pandas.NA and the supplied object ndarray contains at least one value that is neither a string nor a missing sentinel (as detected by lib.is_string_array with skipna=True). StringArray (python storage) requires pure-string content; numeric or mixed content is rejected at the boundary.","triggerScenarios":"Directly constructing pd.arrays.StringArray(np.array([1, 'a', None], dtype=object)) or passing ints/floats. Calling pd.Series([1,2], dtype='string') normally routes through _from_sequence which coerces, but bypassing it via the constructor hits _validate. Inserting via internals that rebuild from a raw ndarray.","commonSituations":"User reaches for the low-level StringArray constructor instead of pd.array(values, dtype='string') which auto-stringifies. Mixed-type object arrays from CSV reading fed directly. Migration code that built object arrays previously and now targets StringArray.","solutions":["Use pd.array(values, dtype='string') or pd.Series(values, dtype='string') — these coerce non-string scalars to str before validation.","Pre-stringify the input: np.array([str(x) for x in values], dtype=object).","If non-string data is legitimate, use dtype='object' or a categorical instead of 'string'."],"exampleFix":"// before\nimport pandas as pd, numpy as np\npd.arrays.StringArray(np.array([1, 'a'], dtype=object))  # raises ValueError\n\n// after\npd.array([1, 'a'], dtype='string')  # coerces 1 -> '1'","handlingStrategy":"validation","validationCode":"import numpy as np, pandas as pd\ndef build_string_array(values):\n    # _from_sequence coerces; direct constructor does not\n    return pd.array(values, dtype='string')","typeGuard":"import pandas as pd, numpy as np\ndef all_strings_or_na(arr: np.ndarray) -> bool:\n    import pandas._libs.lib as lib\n    return lib.is_string_array(arr, skipna=True)","tryCatchPattern":"null","preventionTips":["Prefer pd.array(values, dtype='string') over pd.arrays.StringArray(...) — the former coerces.","Stringify heterogeneous inputs before construction.","Use dtype='object' when mixed types are intentional."],"tags":["string-array","validation","coercion"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}