pola-rs/polars · error

'float' object cannot be interpreted as a

Error message

'float' object cannot be interpreted as a {python_dtype.__name__!r}

What it means

When building a temporal Series from a fill_value/datetime-like input, `sequence_to_pyseries` rejects float values: floats are not accepted as temporal values because the meaning (epoch units) is ambiguous. A TypeError naming the Python dtype is raised.

Solutions

  1. Convert the float epoch to a datetime object first: datetime.fromtimestamp(v, tz=...).
  2. Use an integer with an explicit cast if epoch semantics are intended: pl.Series([int(v)]).cast(pl.Datetime('s')).
  3. Pass datetime/date/time instances rather than numeric values.

Example fix

// before
pl.Series([1700000000.0], dtype=pl.Datetime)
// after
import datetime
pl.Series([datetime.datetime.fromtimestamp(1700000000)], dtype=pl.Datetime)
Defensive patterns

Strategy: type-guard

Validate before calling

if any(isinstance(v, float) for v in values) and dtype in (pl.Datetime, pl.Date, pl.Time, pl.Duration):
    raise TypeError('float values are not accepted for temporal dtypes')

Type guard

def temporal_compatible(values) -> bool:
    import datetime
    return all(v is None or isinstance(v, (datetime.datetime, datetime.date, datetime.time, int)) for v in values)

Try / catch

try:
    s = pl.Series(values, dtype=pl.Datetime)
except TypeError as e:
    if "cannot be interpreted as a" in str(e):
        s = pl.Series([datetime.datetime.fromtimestamp(v) for v in values], dtype=pl.Datetime)
    else:
        raise

Prevention

When it happens

Trigger: pl.Series(..., dtype=pl.Datetime, fill_value=1.5) style paths or passing a float where a datetime/int is expected for a temporal dtype via the AnyValue builder path.

Common situations: Using timestamps-as-floats (epoch seconds) from JS/JSON APIs; fill values for temporal columns copied from numeric defaults.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/_utils/construction/series.py:245

    if python_dtype is None:
        if value is None:
            constructor = polars_type_to_constructor(Null)
            return constructor(name, values, strict)

        # generic default dtype
        python_dtype = type(value)

    # temporal branch
    if issubclass(python_dtype, tuple(py_temporal_types)):
        if dtype is None:
            dtype = parse_into_dtype(python_dtype)  # construct from integer
        elif dtype in py_temporal_types:
            dtype = parse_into_dtype(dtype)

        values_dtype = None if value is None else try_parse_into_dtype(type(value))
        if values_dtype is not None and values_dtype.is_float():
            msg = f"'float' object cannot be interpreted as a {python_dtype.__name__!r}"
            raise TypeError(
                # we do not accept float values as temporal; if this is
                # required, the caller should explicitly cast to int first.
                msg
            )

        # We use the AnyValue builder to create the datetime array
        # We store the values internally as UTC and set the timezone
        py_series = PySeries.new_from_any_values(name, values, strict)

        time_unit = getattr(dtype, "time_unit", None)
        time_zone = getattr(dtype, "time_zone", None)

        if dtype.is_temporal() and values_dtype == String and dtype != Duration:
            s = wrap_s(py_series).str.strptime(dtype, strict=strict)  # type: ignore[arg-type]
        elif time_unit is not None and values_dtype != Date:
            s = wrap_s(py_series).dt.cast_time_unit(time_unit)
        else:
            s = wrap_s(py_series)

View on GitHub (pinned to fe841f959e)