pola-rs/polars · error · TypeError

expected 'time' to be a Python time or Polars expression, fo

Error message

expected 'time' to be a Python time or Polars expression, found {qualified_type_name(time)!r}

What it means

Expr.dt.combine merges a Date column with a time of day. The 'time' argument must be a datetime.time instance or a polars Expr yielding times; strings, tuples, pandas.Timestamp, or np.datetime64 all fail the isinstance check and raise TypeError. Parse such values before calling.

Source

Thrown at py-polars/src/polars/expr/datetime.py:567

        ...     [
        ...         pl.col("dtm").dt.combine(pl.col("tm")).alias("d1"),
        ...         pl.col("dt").dt.combine(pl.col("tm")).alias("d2"),
        ...         pl.col("dt").dt.combine(time(4, 5, 6)).alias("d3"),
        ...     ]
        ... )
        shape: (2, 3)
        ┌─────────────────────────┬─────────────────────────┬─────────────────────┐
        │ d1                      ┆ d2                      ┆ d3                  │
        │ ---                     ┆ ---                     ┆ ---                 │
        │ datetime[μs]            ┆ datetime[μs]            ┆ datetime[μs]        │
        ╞═════════════════════════╪═════════════════════════╪═════════════════════╡
        │ 2022-12-31 01:02:03.456 ┆ 2022-10-10 01:02:03.456 ┆ 2022-10-10 04:05:06 │
        │ 2023-07-05 07:08:09.101 ┆ 2022-07-05 07:08:09.101 ┆ 2022-07-05 04:05:06 │
        └─────────────────────────┴─────────────────────────┴─────────────────────┘
        """
        if not isinstance(time, (dt.time, pl.Expr)):
            msg = f"expected 'time' to be a Python time or Polars expression, found {qualified_type_name(time)!r}"
            raise TypeError(msg)
        time_pyexpr = parse_into_expression(time)
        return wrap_expr(self._pyexpr.dt_combine(time_pyexpr, time_unit))

    def to_string(self, format: str | None = None) -> Expr:
        """
        Convert a Date/Time/Datetime column into a String column with the given format.

        .. engine-support:: in-memory, streaming, distributed

        .. versionchanged:: 1.15.0
            Added support for the use of "iso:strict" as a format string.
        .. versionchanged:: 1.14.0
            Added support for the `Duration` dtype, and use of "iso" as a format string.

        Parameters
        ----------
        format
            * Format to use, refer to the `chrono strftime documentation

View on GitHub (pinned to df599052da)

Solutions

  1. Parse strings first: datetime.time.fromisoformat('04:05:06')
  2. Build an expression literal: pl.time(4, 5, 6)
  3. Convert pandas Timestamps via .to_pydatetime().time()

Example fix

# before
pl.col('date').dt.combine('04:05:06')  # TypeError

# after
import datetime as dt
pl.col('date').dt.combine(dt.time.fromisoformat('04:05:06'))
# or
pl.col('date').dt.combine(pl.time(4, 5, 6))
Defensive patterns

Strategy: type-guard

Validate before calling

import datetime as dt

if isinstance(time, str):
    time = dt.time.fromisoformat(time)
expr = pl.col('date').dt.combine(time)

Type guard

import datetime as dt
import polars as pl

def is_time_or_expr(t) -> bool:
    return isinstance(t, (dt.time, pl.Expr))

Prevention

When it happens

Trigger: pl.col('date').dt.combine('04:05:06'), .dt.combine((4, 5, 6)), or .dt.combine(pd.Timestamp('04:05:06')).

Common situations: Times read from JSON/YAML config as strings; interop code passing pandas Timestamps; tuple hour/minute/second data from legacy schemas.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/64090d79f411dd2c. Report an issue: GitHub.