pola-rs/polars · error
cannot do arithmetic with Series of dtype: {self.dtype!r} an
Error message
cannot do arithmetic with Series of dtype: {self.dtype!r} and argument of type: {type(other).__name__!r} What it means
Raised in Series._arithmetic (py-polars/src/polars/series/series.py:1199) when the FFI arithmetic kernel lookup fails for the (dtype, operand) pair. After special cases (Expr, None, numpy arrays, timedelta, str/float/date/datetime scalars against non-float Series, Decimal with int/Decimal), polars resolves op_ffi for self.dtype; if the combination has no kernel — e.g. adding an int to a String Series — it raises TypeError naming both the Series dtype and the operand's Python type.
Source
Thrown at py-polars/src/polars/series/series.py:1199
if isinstance(other, int):
pyseries = sequence_to_pyseries(self.name, [other])
_s = self._from_pyseries(pyseries).cast(Decimal(scale=0))._s
else:
_s = sequence_to_pyseries(self.name, [other], dtype=Decimal)
if "rhs" in op_ffi:
return self._from_pyseries(getattr(_s, op_s)(self._s))
else:
return self._from_pyseries(getattr(self._s, op_s)(_s))
else:
other = maybe_cast(other, self.dtype)
f = get_ffi_func(op_ffi, self.dtype, self._s)
if f is None:
msg = (
f"cannot do arithmetic with Series of dtype: {self.dtype!r} and argument"
f" of type: {type(other).__name__!r}"
)
raise TypeError(msg)
return self._from_pyseries(f(other))
@overload
def __add__(self, other: DataFrame) -> DataFrame: ...
@overload
def __add__(self, other: Expr) -> Expr: ...
@overload
def __add__(self, other: Any) -> Self: ...
def __add__(self, other: Any) -> Series | DataFrame | Expr:
if isinstance(other, str):
other = Series("", [other])
elif isinstance(other, pl.DataFrame):
return other + self
elif isinstance(other, pl.Expr):
return F.lit(self) + otherView on GitHub (pinned to df599052da)
Solutions
- Fix the dtype first: s = s.str.strip().cast(pl.Int64) for numeric strings, then s + 1
- String concatenation uses a str operand: s + "x"; for repetition use s * 2 only where supported — otherwise s.str.repeat(2)
- For nested dtypes use the namespace: s.list.eval(...) / s.arr.* instead of scalar operators
- For Decimal Series keep operands int or decimal.Decimal (not float), or cast the Series to Float64 first
Example fix
# before s = pl.Series(["1", "2"]) s + 1 # TypeError: String + int # after s.cast(pl.Int64) + 1
Defensive patterns
Strategy: validation
Validate before calling
import polars as pl
def numeric_op_safe(s: pl.Series, other) -> bool:
return s.dtype.is_numeric() and isinstance(other, (int, float)) or (
s.dtype == pl.String and isinstance(other, str)
)
if not numeric_op_safe(s, 1):
raise TypeError(f"arithmetic on {s.dtype} with {type(other).__name__} not supported; cast first") Type guard
def is_arithmetic_ready(s: pl.Series, other: object) -> bool:
if s.dtype.is_numeric():
return isinstance(other, (int, float)) or hasattr(other, "_s")
if s.dtype == pl.String:
return isinstance(other, str)
return s.dtype.is_temporal() # date/datetime/duration have kernels for their own kinds Try / catch
try:
out = s + 1
except TypeError as e:
if "cannot do arithmetic" in str(e) and s.dtype == pl.String:
out = s.cast(pl.Int64) + 1 # or s.str.to_datetime() etc. per data
else:
raise Prevention
- Check s.dtype after ingestion; cast numeric-looking String columns before math
- Keep Decimal Series away from float operands (use int or decimal.Decimal)
- For nested dtypes use list/arr namespaces instead of scalar operators
When it happens
Trigger: pl.Series(["a", "b"]) + 1 (string + int); pl.Series([[1, 2]]) * 2 (List arithmetic with a scalar); duration arithmetic in an unsupported direction; Decimal Series combined with a float (only int/PyDecimal are special-cased); Object Series with any operator.
Common situations: Type drift again — numeric-looking columns parsed as String doing `s + 1`; multiplying list columns expecting broadcasting; mixing Decimal Series with floats. Also porting pandas code where some of these ops silently worked (string repetition via *, elementwise list ops).
Related errors
- first cast to integer before dividing datelike dtypes
- first cast to integer before multiplying datelike dtypes
- first cast to integer before applying modulo on datelike dty
- cannot treat Series of type {s.dtype} as indices
- cannot treat NumPy array of type {arr.dtype} as indices
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/5c7da5edd88c8f68.
Report an issue: GitHub.