pandas-dev/pandas · error · TypeError
bad operand type for unary +: '{self.dtype}'
Error message
bad operand type for unary +: '{self.dtype}' What it means
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.
Source
Thrown at pandas/core/arrays/string_arrow.py:657
def _cmp_method(self, other, op):
if (
isinstance(other, (BaseStringArray, ArrowExtensionArray))
and self.dtype.na_value is not libmissing.NA
and other.dtype.na_value is libmissing.NA
):
# NA has priority of NaN semantics
return NotImplemented
result = super()._cmp_method(other, op)
if self.dtype.na_value is np.nan:
if op == operator.ne:
return result.to_numpy(np.bool_, na_value=True)
else:
return result.to_numpy(np.bool_, na_value=False)
return result
def __pos__(self) -> Self:
raise TypeError(f"bad operand type for unary +: '{self.dtype}'")
View on GitHub (pinned to 71959b8cb9)
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.
Example fix
# before
s = pd.Series(['1','2'], dtype='string[pyarrow]')
result = +s # TypeError
# after
result = s.astype('int64') Defensive patterns
Strategy: type-guard
Validate before calling
import pandas as pd
def safe_unary_plus(s):
if pd.api.types.is_string_dtype(s):
raise TypeError('unary + not valid on string dtype')
return +s Type guard
import pandas as pd
def supports_unary_plus(s) -> bool:
return pd.api.types.is_numeric_dtype(s) Try / catch
try:
return +s
except TypeError as e:
if 'bad operand type for unary' in str(e):
return s.astype('float64')
raise Prevention
- Avoid unary + in generic pipelines; branch on dtype.
- Use explicit astype for numeric coercion.
- Test operators against each dtype kind in your suite.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid value '{item}' for dtype 'str'. Value should be a st
- Invalid value '{value}' for dtype 'str'. Value should be a s
- Invalid value for dtype 'str'. Value should be a string or m
- Cannot perform reduction '{name}' with string dtype
- Unordered Categoricals can only compare equality or not
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/066a8d1b76c903b3.
Report an issue: GitHub.