pola-rs/polars · error · TypeError

first cast to integer before dividing datelike dtypes

Error message

first cast to integer before dividing datelike dtypes

What it means

Series.__truediv__ refuses to divide temporal dtypes (Date, Datetime, Time — but not Duration) because dividing a calendar date by a number has no well-defined meaning. Polars requires casting the temporal value to a numeric type first, making the intent explicit. Duration is exempt because dividing a duration by a number is meaningful.

Solutions

  1. Cast to integer before dividing: s.cast(pl.Int64) / divisor, then re-cast to a temporal type if needed (e.g. .cast(pl.Datetime('s'))).
  2. For unit conversion use dedicated temporal APIs: s.dt.cast_time_unit('s') for Datetime, or s.dt.epoch(time_unit='s').
  3. For 'days since epoch', compute s.dt.epoch(time_unit='d') rather than dividing the Datetime.
  4. Duration Series divided by numbers is allowed; keep the Duration dtype if scaling a duration is the goal.

Example fix

// before
s = pl.Series('ts', [datetime(2024, 1, 1)])  # dtype Datetime
s / 1_000_000  # TypeError: first cast to integer before dividing datelike dtypes

// after
s.cast(pl.Int64) / 1_000_000  # or s.dt.cast_time_unit('ms')
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 (or use .dt accessors) before dividing'

Type guard

def dividable(dtype: pl.DataType) -> bool:
    return not dtype.is_temporal() or isinstance(dtype, pl.Duration)

Try / catch

try:
    result = s / other
except TypeError as e:
    if 'first cast to integer before dividing datelike' in str(e):
        result = s.cast(pl.Int64) / other
    else:
        raise

Prevention

When it happens

Trigger: date_series / 2 or datetime_series / 1_000_000, where the Series dtype is Date, Datetime or Time (is_temporal() is True and the dtype is not Duration). The intent is usually to scale epoch values or convert timestamp units.

Common situations: Converting timestamps between units (us -> s) by dividing a Datetime column; porting pandas/numpy code where datetime64 arithmetic silently produced numbers; computing 'days since epoch' directly on a Date column.

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


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/739445bd10e3a6a3. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/series/series.py:1148

                return Array(convert_to_primitive(dtype.inner), shape=dtype.shape)
            if isinstance(dtype, List):
                return List(convert_to_primitive(dtype.inner))
            return leaf_dtype

        return self.cast(convert_to_primitive(self.dtype))

    @overload
    def __truediv__(self, other: Expr) -> Expr: ...

    @overload
    def __truediv__(self, other: Any) -> Series: ...

    def __truediv__(self, other: Any) -> Series | 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 dividing 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()

        self = (
            self
            if (
                self.dtype.is_float()
                or self.dtype.is_decimal()
                or isinstance(self.dtype, (List, Array, Duration))
                or (
                    isinstance(other, Series) and isinstance(other.dtype, (List, Array))
                )
            )
            else self._recursive_cast_to_dtype(Float64())
        )

View on GitHub (pinned to fe841f959e)