{"record":{"id":"660b5b4c4e953ab3","repo":"pandas-dev/pandas","slug":"stringarray-requires-a-sequence-of-strings-or-nan-660b5b","errorCode":null,"errorMessage":"StringArray requires a sequence of strings or NaN. Got '{self._ndarray.dtype}' dtype instead.","messagePattern":"StringArray requires a sequence of strings or NaN\\. Got '(.+?)' dtype instead\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":744,"sourceCode":"            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            ):\n                raise ValueError(\"StringArray requires a sequence of strings or NaN\")\n            if self._ndarray.dtype != \"object\":\n                raise ValueError(\n                    \"StringArray requires a sequence of strings \"\n                    \"or NaN. Got '{self._ndarray.dtype}' dtype instead.\"\n                )\n            # TODO validate or force NA/None to NaN\n\n    def _validate_scalar(self, value):\n        # used by NDArrayBackedExtensionIndex.insert\n        if isna(value):\n            return self.dtype.na_value\n        elif not isinstance(value, str):\n            raise TypeError(\n                f\"Invalid value '{value}' for dtype '{self.dtype}'. Value should be a \"\n                f\"string or missing value, got '{type(value).__name__}' instead.\"\n            )\n        return value\n\n    @classmethod\n    def _from_sequence(","sourceCodeStart":726,"sourceCodeEnd":762,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L726-L762","documentation":"NaN-semantics counterpart of error 428: after the content check, _validate requires the ndarray dtype to be object for the np.nan-na_value variant. A typed ndarray (int64, float64, '<U') is rejected. Note: the source string at line 744 is missing the f-string prefix, so the literal text '{self._ndarray.dtype}' appears un-interpolated in the message.","triggerScenarios":"Constructing StringArray(np.array([1, 2, 3]), dtype=StringDtype(na_value=np.nan)), or passing a '<U' or numeric ndarray under NaN semantics.","commonSituations":"Using the experimental 'str' dtype with raw numpy arrays not cast to object.","solutions":["Cast the ndarray to object dtype first: np.array(values, dtype=object).","Use pd.array(values, dtype='str') which handles the dtype internally.","Convert '<U' arrays via .astype(object)."],"exampleFix":"// before\narr = pd.arrays.StringArray(np.array(['a','b']), dtype=pd.StringDtype(na_value=np.nan))\n\n// after\narr = pd.array(['a','b'], dtype='str')","handlingStrategy":"validation","validationCode":"import numpy as np\nvalues = np.asarray(values)\nif values.dtype != object:\n    values = values.astype(object)\narr = pd.array(values, dtype='str')","typeGuard":"import numpy as np\n\ndef is_object_ndarray(values) -> bool:\n    return isinstance(values, np.ndarray) and values.dtype == object","tryCatchPattern":null,"preventionTips":["Cast ndarrays to object dtype before constructing a NaN-semantics StringArray.","Use pd.array(values, dtype='str') which manages dtype internally.","Avoid passing '<U' or numeric ndarrays directly."],"tags":["string-array","dtype","validation","nan-semantics","construction"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}