{"record":{"id":"3ec8e507a5945e5a","repo":"pandas-dev/pandas","slug":"stringarray-requires-a-sequence-of-strings-or-pand-3ec8e5","errorCode":null,"errorMessage":"StringArray requires a sequence of strings or pandas.NA. Got '{self._ndarray.dtype}' dtype instead.","messagePattern":"StringArray requires a sequence of strings or pandas\\.NA\\. Got '(.+?)' dtype instead\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":727,"sourceCode":"            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            ):\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 \"","sourceCodeStart":709,"sourceCodeEnd":745,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L709-L745","documentation":"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.","triggerScenarios":"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.","commonSituations":"Passing numpy arrays created without dtype=object; assuming StringArray accepts unicode ('<U') ndarrays directly.","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."],"exampleFix":"// before\narr = pd.arrays.StringArray(np.array(['a','b']))\n\n// after\narr = pd.arrays.StringArray(np.array(['a','b'], dtype=object))","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='string')","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":["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."],"tags":["string-array","dtype","validation","construction"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}