{"record":{"id":"083f5435273afde7","repo":"pola-rs/polars","slug":"float-object-cannot-be-interpreted-as-a-python","errorCode":null,"errorMessage":"'float' object cannot be interpreted as a {python_dtype.__name__!r}","messagePattern":"'float' object cannot be interpreted as a (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/series.py","lineNumber":245,"sourceCode":"    if python_dtype is None:\n        if value is None:\n            constructor = polars_type_to_constructor(Null)\n            return constructor(name, values, strict)\n\n        # generic default dtype\n        python_dtype = type(value)\n\n    # temporal branch\n    if issubclass(python_dtype, tuple(py_temporal_types)):\n        if dtype is None:\n            dtype = parse_into_dtype(python_dtype)  # construct from integer\n        elif dtype in py_temporal_types:\n            dtype = parse_into_dtype(dtype)\n\n        values_dtype = None if value is None else try_parse_into_dtype(type(value))\n        if values_dtype is not None and values_dtype.is_float():\n            msg = f\"'float' object cannot be interpreted as a {python_dtype.__name__!r}\"\n            raise TypeError(\n                # we do not accept float values as temporal; if this is\n                # required, the caller should explicitly cast to int first.\n                msg\n            )\n\n        # We use the AnyValue builder to create the datetime array\n        # We store the values internally as UTC and set the timezone\n        py_series = PySeries.new_from_any_values(name, values, strict)\n\n        time_unit = getattr(dtype, \"time_unit\", None)\n        time_zone = getattr(dtype, \"time_zone\", None)\n\n        if dtype.is_temporal() and values_dtype == String and dtype != Duration:\n            s = wrap_s(py_series).str.strptime(dtype, strict=strict)  # type: ignore[arg-type]\n        elif time_unit is not None and values_dtype != Date:\n            s = wrap_s(py_series).dt.cast_time_unit(time_unit)\n        else:\n            s = wrap_s(py_series)","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/series.py#L227-L263","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using timestamps-as-floats (epoch seconds) from JS/JSON APIs; fill values for temporal columns copied from numeric defaults.","solutions":["Convert the float epoch to a datetime object first: datetime.fromtimestamp(v, tz=...).","Use an integer with an explicit cast if epoch semantics are intended: pl.Series([int(v)]).cast(pl.Datetime('s')).","Pass datetime/date/time instances rather than numeric values."],"exampleFix":"// before\npl.Series([1700000000.0], dtype=pl.Datetime)\n// after\nimport datetime\npl.Series([datetime.datetime.fromtimestamp(1700000000)], dtype=pl.Datetime)","handlingStrategy":"type-guard","validationCode":"if any(isinstance(v, float) for v in values) and dtype in (pl.Datetime, pl.Date, pl.Time, pl.Duration):\n    raise TypeError('float values are not accepted for temporal dtypes')","typeGuard":"def temporal_compatible(values) -> bool:\n    import datetime\n    return all(v is None or isinstance(v, (datetime.datetime, datetime.date, datetime.time, int)) for v in values)","tryCatchPattern":"try:\n    s = pl.Series(values, dtype=pl.Datetime)\nexcept TypeError as e:\n    if \"cannot be interpreted as a\" in str(e):\n        s = pl.Series([datetime.datetime.fromtimestamp(v) for v in values], dtype=pl.Datetime)\n    else:\n        raise","preventionTips":["Convert epoch floats to datetime objects at the API boundary.","Never use floats as fill values for temporal columns.","Document that timestamps arrive as ISO strings or datetimes, not floats."],"tags":["polars","datetime","type-mismatch"],"backgroundTag":"invalid-date-format","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"}