{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_.py#L709-L745","documentation":"Thrown by StringArray._validate in pandas/core/arrays/string_.py:727 when na_value is pandas.NA and the backing ndarray's dtype is not 'object'. StringArray stores strings as Python objects in an object-dtype ndarray; a numeric/structured dtype means the data was not prepared correctly, so construction is rejected before any string operation runs.","triggerScenarios":"Constructing pd.arrays.StringArray(np.array([1,2,3])) (int dtype ndarray) directly. Passing a float64 ndarray from a numeric column to the StringArray constructor without object conversion. Internal code that hands a non-object ndarray to StringArray.","commonSituations":"User assumes StringArray casts numeric arrays the way pd.Series(..., dtype='string') does — the low-level constructor does not. Reusing a numeric buffer for a string column without an intermediate astype(object).","solutions":["Use pd.array(numeric_arr, dtype='string') which handles conversion through _from_sequence.","Convert to object dtype first: pd.arrays.StringArray(numeric_arr.astype(object)) — but note non-string objects still trip error 452.","Cast numerics to strings explicitly: numeric_arr.astype(str).astype(object)."],"exampleFix":"// before\nimport pandas as pd, numpy as np\npd.arrays.StringArray(np.array([1,2,3]))  # raises ValueError\n\n// after\npd.array([1,2,3], dtype='string')","handlingStrategy":"validation","validationCode":"import numpy as np\ndef ensure_object_ndarray(values):\n    arr = np.asarray(values)\n    if arr.dtype != object:\n        arr = arr.astype(object)\n    return arr","typeGuard":"import numpy as np\ndef is_object_ndarray(arr) -> bool:\n    return getattr(arr, 'dtype', None) == object","tryCatchPattern":"null","preventionTips":["Convert numeric ndarrays to strings via .astype(str) before handing to StringArray.","Use pd.array(..., dtype='string') which handles dtype conversion internally.","Do not bypass _from_sequence with the raw constructor unless data is already object-dtype strings."],"tags":["string-array","dtype","validation"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}