{"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":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/series/series.py","lineNumber":1205,"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":1187,"sourceCodeEnd":1223,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/series/series.py#L1187-L1223","documentation":"Series.__mul__ blocks multiplication of temporal dtypes (Date, Datetime, Time — not Duration), since multiplying a calendar date by a number has no defined meaning. As with division, polars requires an explicit cast to a numeric dtype. Duration Series may be multiplied by numbers because scaling a duration is valid.","triggerScenarios":"date_series * 2 or datetime_series * 1000, where self.dtype.is_temporal() and the dtype is not Duration; multiplying a Time column by a factor; porting numpy code that treated dates as int64 nanoseconds.","commonSituations":"Scaling timestamp offsets computed from Datetime columns; pandas-to-polars ports where datetime64 columns were multiplied as integers; attempts to build shifted timestamps via multiplication instead of offset arithmetic.","solutions":["Cast to integer first: s.cast(pl.Int64) * factor, then re-cast to a temporal type afterwards if needed.","For shifted timestamps use temporal arithmetic: s + pl.duration(days=1) or s.dt.offset_by('1d').","Duration Series multiplied by int/float is allowed and stays Duration; keep the dtype rather than casting if scaling is the goal.","Expression form works after casting: s.cast(pl.Int64).to_frame().select(pl.col(name) * other)."],"exampleFix":"// before\ns = pl.Series('ts', [datetime(2024, 1, 1)])  # dtype Datetime\ns * 2  # TypeError: first cast to integer before multiplying datelike dtypes\n\n// after\ns.cast(pl.Int64) * 2  # or s + pl.duration(days=1) for offsetting","handlingStrategy":"validation","validationCode":"assert not (s.dtype.is_temporal() and not isinstance(s.dtype, pl.Duration)), \\\n    f'cast {s.dtype} to Int64 before multiplying'","typeGuard":"def multipliable(dtype: pl.DataType) -> bool:\n    return not dtype.is_temporal() or isinstance(dtype, pl.Duration)","tryCatchPattern":"try:\n    result = s * factor\nexcept TypeError as e:\n    if 'first cast to integer before multiplying datelike' in str(e):\n        result = s.cast(pl.Int64) * factor\n    else:\n        raise","preventionTips":["Do not treat Datetime/Date columns as integers; use dt.epoch or cast explicitly first.","Offset timestamps with pl.duration / dt.offset_by instead of multiplication.","Keep Duration columns for scaling operations rather than converting to raw integers early.","Assert dtypes in data-processing functions to catch temporal columns before arithmetic."],"tags":["python","polars","temporal","multiplication","dtype"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}