{"record":{"id":"066a8d1b76c903b3","repo":"pandas-dev/pandas","slug":"bad-operand-type-for-unary-self-dtype","errorCode":null,"errorMessage":"bad operand type for unary +: '{self.dtype}'","messagePattern":"bad operand type for unary \\+: '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/string_arrow.py","lineNumber":657,"sourceCode":"    def _cmp_method(self, other, op):\n        if (\n            isinstance(other, (BaseStringArray, ArrowExtensionArray))\n            and self.dtype.na_value is not libmissing.NA\n            and other.dtype.na_value is libmissing.NA\n        ):\n            # NA has priority of NaN semantics\n            return NotImplemented\n\n        result = super()._cmp_method(other, op)\n        if self.dtype.na_value is np.nan:\n            if op == operator.ne:\n                return result.to_numpy(np.bool_, na_value=True)\n            else:\n                return result.to_numpy(np.bool_, na_value=False)\n        return result\n\n    def __pos__(self) -> Self:\n        raise TypeError(f\"bad operand type for unary +: '{self.dtype}'\")\n","sourceCodeStart":639,"sourceCodeEnd":658,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/string_arrow.py#L639-L658","documentation":"Raised by ArrowStringArray.__pos__ (unary + operator). Applying unary plus to a string array has no meaningful numeric result, so pandas raises a TypeError mirroring Python's own 'bad operand type for unary +'. This mirrors numpy/object behavior and prevents silent no-ops when code written for numeric arrays is applied to strings.","triggerScenarios":"Writing `+s` or `+df['col']` where s/col has dtype 'string[pyarrow]'. Also triggered by libraries (e.g. some expression engines) that apply unary plus generically to all columns.","commonSituations":"Generic vectorized pipelines that prefix + to 'ensure numeric'; copy-paste from numeric code into a string context; expression-tree evaluators that visit every column with unary operators.","solutions":["Remove the unary + operator on string columns; it has no effect you want.","If the intent was to coerce to numeric, use `s.astype('float64')` or `pd.to_numeric(s)` explicitly.","Branch on dtype before applying unary operators so string columns are skipped."],"exampleFix":"# before\ns = pd.Series(['1','2'], dtype='string[pyarrow]')\nresult = +s  # TypeError\n# after\nresult = s.astype('int64')","handlingStrategy":"type-guard","validationCode":"import pandas as pd\n\ndef safe_unary_plus(s):\n    if pd.api.types.is_string_dtype(s):\n        raise TypeError('unary + not valid on string dtype')\n    return +s","typeGuard":"import pandas as pd\ndef supports_unary_plus(s) -> bool:\n    return pd.api.types.is_numeric_dtype(s)","tryCatchPattern":"try:\n    return +s\nexcept TypeError as e:\n    if 'bad operand type for unary' in str(e):\n        return s.astype('float64')\n    raise","preventionTips":["Avoid unary + in generic pipelines; branch on dtype.","Use explicit astype for numeric coercion.","Test operators against each dtype kind in your suite."],"tags":["string-arrow","unary-operator","typeerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}