{"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/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/string_.py#L726-L762","documentation":"Thrown by StringArray._validate in pandas/core/arrays/string_.py:744 — the NaN-flavored counterpart of error 453. Fires when na_value is np.nan and the backing ndarray dtype is not 'object'. Note: the source string at line 746 is missing the 'f' prefix, so the placeholder '{self._ndarray.dtype}' is emitted literally rather than interpolated; this is a known minor bug in the message formatting and does not affect the exception type.","triggerScenarios":"Constructing pd.arrays.StringArray(np.array([1,2,3]), dtype=StringDtype(na_value=np.nan)) — numeric ndarray with the NaN-variant dtype. Triggered via the infer_string future when a non-object ndarray reaches the constructor.","commonSituations":"Same as error 453 but in code paths where the NaN-flavored StringDtype is selected (e.g., pd.options.future.infer_string = True and dtype=str).","solutions":["Use pd.array(numeric_arr, dtype=str) which converts via _from_sequence.","Cast to object first: numeric_arr.astype(object) — but ensure contents are strings or NaN to avoid error 454.","Cast numerics to strings: numeric_arr.astype(str).astype(object)."],"exampleFix":"// before\nimport pandas as pd, numpy as np\ndtype = pd.StringDtype(storage='python', na_value=np.nan)\npd.arrays.StringArray(np.array([1,2,3]), dtype=dtype)  # raises ValueError\n\n// after\npd.array([1,2,3], dtype=str)","handlingStrategy":"validation","validationCode":"import numpy as np\ndef to_object_string_array(values):\n    arr = np.asarray(values)\n    if arr.dtype != object:\n        arr = arr.astype(str).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":["Always go through pd.array(values, dtype=str) to let pandas handle the conversion chain.","When constructing the NaN variant directly, convert numeric arrays to object dtype first.","Note the message has a formatting bug (placeholder not interpolated) — rely on the dtype check, not the text."],"tags":["string-array","dtype","validation","nan","bug-in-message"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}