{"record":{"id":"280f3cf72e42529b","repo":"pola-rs/polars","slug":"first-cast-to-integer-before-multiplying-datelike","errorCode":null,"errorMessage":"first cast to integer before multiplying datelike dtypes","messagePattern":"first cast to integer before multiplying datelike dtypes","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1319,"sourceCode":"\n    def __invert__(self) -> Series:\n        return self.not_()\n\n    @overload\n    def __mul__(self, other: Expr) -> Expr: ...\n\n    @overload\n    def __mul__(self, other: DataFrame) -> DataFrame: ...\n\n    @overload\n    def __mul__(self, other: Any) -> Series: ...\n\n    def __mul__(self, other: Any) -> Series | DataFrame | Expr:\n        if isinstance(other, pl.Expr):\n            return F.lit(self) * other\n        if self.dtype.is_temporal() and not isinstance(self.dtype, Duration):\n            msg = \"first cast to integer before multiplying datelike dtypes\"\n            raise TypeError(msg)\n        if isinstance(other, (int, float)) and (\n            self.dtype.is_decimal() or isinstance(self.dtype, Duration)\n        ):\n            return self.to_frame().select(F.col(self.name) * other).to_series()\n        elif isinstance(other, pl.DataFrame):\n            return other * self\n        else:\n            return self._arithmetic(other, \"mul\", \"mul_<>\")\n\n    @overload\n    def __mod__(self, other: Expr) -> Expr: ...\n\n    @overload\n    def __mod__(self, other: Any) -> Series: ...\n\n    def __mod__(self, other: Any) -> Series | Expr:\n        if isinstance(other, pl.Expr):\n            return F.lit(self).__mod__(other)","sourceCodeStart":1301,"sourceCodeEnd":1337,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/series/series.py#L1301-L1337","documentation":"Raised by Series.__mul__ when multiplying a datelike Series (Date, Datetime, Time) by anything, unless the left operand is a Duration. Multiplying a calendar value by a scalar has no meaningful result, so Polars rejects it; Duration * n (scaling a timespan) is allowed and dispatched through the expression engine.","triggerScenarios":"Calling `*` on a Series where self.dtype.is_temporal() and not Duration, with a non-Expr right operand: `date_series * 2`, `datetime_series * 1.5`. Also fires for `s * pl.DataFrame(...)`? No - the DataFrame branch is checked after the raise, so `date_series * df` also raises here.","commonSituations":"Attempting to scale epoch-like columns that arrived as Date/Datetime; converting 'days since epoch' style columns where the dtype drifted to Date during CSV inference; copying unit-test math from integer columns onto date columns.","solutions":["Cast to integer before multiplying: `s.cast(pl.Int64) * n`.","Use dt accessors for the quantity you actually want scaled: `s.dt.epoch('d') * n`.","If you intended to shift a date rather than scale it, add a duration instead: `s + pl.duration(days=n)`.","Fix upstream dtype: specify `schema_overrides` at read time so the column stays numeric."],"exampleFix":"// before\ndates = pl.Series([date(2024,1,1)]).cast(pl.Date)\ndates * 2  # TypeError\n\n// after\ndates.cast(pl.Int64) * 2\n# shifting, not scaling:\ndates + pl.duration(days=2)","handlingStrategy":"type-guard","validationCode":"if s.dtype.is_temporal() and not isinstance(s.dtype, pl.Duration):\n    s = s.cast(pl.Int64)\nresult = s * n","typeGuard":"def supports_mul(s: pl.Series) -> bool:\n    return not (s.dtype.is_temporal() and not isinstance(s.dtype, pl.Duration))","tryCatchPattern":"try:\n    out = s * n\nexcept TypeError as e:\n    if 'multiplying datelike' not in str(e):\n        raise\n    out = s.cast(pl.Int64) * n","preventionTips":["For 'shift date' intent use s + pl.duration(...) instead of multiplication.","Only Duration may be scaled by a scalar - convert to Duration first when scaling timespans."],"tags":["polars","series","arithmetic","temporal","dtype","mul"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}