pola-rs/polars · error · TypeError
first cast to integer before multiplying datelike dtypes
Error message
first cast to integer before multiplying datelike dtypes
What it means
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.
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).
Example fix
// before
s = pl.Series('ts', [datetime(2024, 1, 1)]) # dtype Datetime
s * 2 # TypeError: first cast to integer before multiplying datelike dtypes
// after
s.cast(pl.Int64) * 2 # or s + pl.duration(days=1) for offsetting Defensive patterns
Strategy: validation
Validate before calling
assert not (s.dtype.is_temporal() and not isinstance(s.dtype, pl.Duration)), \
f'cast {s.dtype} to Int64 before multiplying' Type guard
def multipliable(dtype: pl.DataType) -> bool:
return not dtype.is_temporal() or isinstance(dtype, pl.Duration) Try / catch
try:
result = s * factor
except TypeError as e:
if 'first cast to integer before multiplying datelike' in str(e):
result = s.cast(pl.Int64) * factor
else:
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- first cast to integer before applying modulo on datelike…
- first cast to integer before dividing datelike dtypes
- cannot compare datetime.datetime to Series of type
- cannot construct PySeries for type
- cannot convert DataFrame to
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/280f3cf72e42529b.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/series/series.py:1205
def __invert__(self) -> Series:
return self.not_()
@overload
def __mul__(self, other: Expr) -> Expr: ...
@overload
def __mul__(self, other: DataFrame) -> DataFrame: ...
@overload
def __mul__(self, other: Any) -> Series: ...
def __mul__(self, other: Any) -> Series | DataFrame | Expr:
if isinstance(other, pl.Expr):
return F.lit(self) * other
if self.dtype.is_temporal() and not isinstance(self.dtype, Duration):
msg = "first cast to integer before multiplying datelike dtypes"
raise TypeError(msg)
if isinstance(other, (int, float)) and (
self.dtype.is_decimal() or isinstance(self.dtype, Duration)
):
return self.to_frame().select(F.col(self.name) * other).to_series()
elif isinstance(other, pl.DataFrame):
return other * self
else:
return self._arithmetic(other, "mul", "mul_<>")
@overload
def __mod__(self, other: Expr) -> Expr: ...
@overload
def __mod__(self, other: Any) -> Series: ...
def __mod__(self, other: Any) -> Series | Expr:
if isinstance(other, pl.Expr):
return F.lit(self).__mod__(other)View on GitHub (pinned to fe841f959e)