{"record":{"id":"7418c00975b63c5f","repo":"pandas-dev/pandas","slug":"stringarray-requires-a-sequence-of-strings-or-nan","errorCode":null,"errorMessage":"StringArray requires a sequence of strings or NaN","messagePattern":"StringArray requires a sequence of strings or NaN","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_.py","lineNumber":742,"sourceCode":"                    \"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 \"\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","sourceCodeStart":724,"sourceCodeEnd":760,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_.py#L724-L760","documentation":"The NaN-semantics branch of StringArray._validate (used when dtype.na_value is np.nan, i.e. the 'str' dtype). It calls lib.is_string_array(ndarray, skipna=True) and raises ValueError if any non-NaN element is not a str. Identical logic to the NA branch but for the NumPy-semantics string dtype.","triggerScenarios":"Constructing StringArray(np.array([1, 'a'], dtype=object), dtype=StringDtype(na_value=np.nan)), or using the experimental 'str' dtype with mixed non-string content.","commonSituations":"Opting into NumPy string semantics (using_string_dtype) and feeding uncoerced mixed-type object arrays.","solutions":["Pre-convert all non-missing values to str.","Use pd.array(values, dtype='str') or _from_sequence which coerces.","Ensure only str and np.nan/None appear in the object array."],"exampleFix":"// before\narr = pd.arrays.StringArray(np.array([1, 'a'], dtype=object), dtype=pd.StringDtype(na_value=np.nan))\n\n// after\narr = pd.array([1, 'a'], dtype='str')","handlingStrategy":"validation","validationCode":"clean = [str(v) if not (v is None or v is pd.NA) else v for v in values]\narr = pd.array(clean, dtype='str')","typeGuard":"import pandas as pd\nimport numpy as np\n\ndef all_strings_or_nan(values) -> bool:\n    return all(isinstance(v, str) or v is None or (isinstance(v, float) and np.isnan(v)) for v in values)","tryCatchPattern":null,"preventionTips":["Coerce all non-missing values to str before construction under NaN semantics.","Use pd.array(values, dtype='str') or _from_sequence which coerces.","Keep only str and NaN/None in the input."],"tags":["string-array","validation","nan-semantics","construction"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}