{"record":{"id":"5c7da5edd88c8f68","repo":"pola-rs/polars","slug":"cannot-do-arithmetic-with-series-of-dtype-self-d","errorCode":null,"errorMessage":"cannot do arithmetic with Series of dtype: {self.dtype!r} and argument of type: {type(other).__name__!r}","messagePattern":"cannot do arithmetic with Series of dtype: (.+?) and argument of type: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1199,"sourceCode":"            if isinstance(other, int):\n                pyseries = sequence_to_pyseries(self.name, [other])\n                _s = self._from_pyseries(pyseries).cast(Decimal(scale=0))._s\n            else:\n                _s = sequence_to_pyseries(self.name, [other], dtype=Decimal)\n\n            if \"rhs\" in op_ffi:\n                return self._from_pyseries(getattr(_s, op_s)(self._s))\n            else:\n                return self._from_pyseries(getattr(self._s, op_s)(_s))\n        else:\n            other = maybe_cast(other, self.dtype)\n            f = get_ffi_func(op_ffi, self.dtype, self._s)\n        if f is None:\n            msg = (\n                f\"cannot do arithmetic with Series of dtype: {self.dtype!r} and argument\"\n                f\" of type: {type(other).__name__!r}\"\n            )\n            raise TypeError(msg)\n        return self._from_pyseries(f(other))\n\n    @overload\n    def __add__(self, other: DataFrame) -> DataFrame: ...\n\n    @overload\n    def __add__(self, other: Expr) -> Expr: ...\n\n    @overload\n    def __add__(self, other: Any) -> Self: ...\n\n    def __add__(self, other: Any) -> Series | DataFrame | Expr:\n        if isinstance(other, str):\n            other = Series(\"\", [other])\n        elif isinstance(other, pl.DataFrame):\n            return other + self\n        elif isinstance(other, pl.Expr):\n            return F.lit(self) + other","sourceCodeStart":1181,"sourceCodeEnd":1217,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/series.py#L1181-L1217","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"# before\ns = pl.Series([\"1\", \"2\"])\ns + 1  # TypeError: String + int\n\n# after\ns.cast(pl.Int64) + 1","handlingStrategy":"validation","validationCode":"import polars as pl\n\ndef numeric_op_safe(s: pl.Series, other) -> bool:\n    return s.dtype.is_numeric() and isinstance(other, (int, float)) or (\n        s.dtype == pl.String and isinstance(other, str)\n    )\n\nif not numeric_op_safe(s, 1):\n    raise TypeError(f\"arithmetic on {s.dtype} with {type(other).__name__} not supported; cast first\")","typeGuard":"def is_arithmetic_ready(s: pl.Series, other: object) -> bool:\n    if s.dtype.is_numeric():\n        return isinstance(other, (int, float)) or hasattr(other, \"_s\")\n    if s.dtype == pl.String:\n        return isinstance(other, str)\n    return s.dtype.is_temporal()  # date/datetime/duration have kernels for their own kinds","tryCatchPattern":"try:\n    out = s + 1\nexcept TypeError as e:\n    if \"cannot do arithmetic\" in str(e) and s.dtype == pl.String:\n        out = s.cast(pl.Int64) + 1  # or s.str.to_datetime() etc. per data\n    else:\n        raise","preventionTips":["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"],"tags":["polars","series","arithmetic","dtype","typeerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}