{"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":238,"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":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/construction/series.py#L220-L256","documentation":"Polars temporal types (Date, Datetime, Duration, Time) are stored as integers, and constructing them from Python/NumPy values explicitly rejects floats: a float epoch could be silently truncated, corrupting timestamps. The source comment states the caller must cast to int first; violations raise this TypeError naming the target type.","triggerScenarios":"pl.Series([1.5e9, 1.6e9], dtype=pl.Datetime(\"us\")); np.array([...], dtype=\"float64\") passed with dtype=pl.Date; epoch values from JSON APIs arriving as floats.","commonSituations":"Unix timestamps returned as floats by APIs/databases; NumPy epoch arrays that default to float64; unit tests using round numbers like 1700000000.0.","solutions":["Convert to integers first: [int(v) for v in values] or values.astype(\"int64\").","Use the dedicated epoch helper: pl.from_epoch(pl.Series(values).cast(pl.Int64), time_unit=\"s\").","If fractional seconds matter, scale then truncate: (values * 1_000_000).astype(\"int64\") for microsecond Datetime.","Or pass real datetime objects and let polars convert."],"exampleFix":"// before\ns = pl.Series(\"ts\", [1700000000.0, 1700000060.0], dtype=pl.Datetime(\"us\"))\n\n// after\ns = pl.from_epoch(pl.Series(\"ts\", [1700000000, 1700000060]), time_unit=\"s\")","handlingStrategy":"type-guard","validationCode":"import numbers\n\nif any(isinstance(v, float) for v in values):\n    values = [int(v) for v in values]  # caller must decide truncation policy\ns = pl.Series(name, values, dtype=pl.Datetime(\"us\"))","typeGuard":"def has_no_floats(values) -> bool:\n    return not any(isinstance(v, numbers.Real) and not isinstance(v, numbers.Integral) for v in values)","tryCatchPattern":"try:\n    s = pl.Series(name, values, dtype=target_temporal_dtype)\nexcept TypeError as e:\n    if \"cannot be interpreted as\" in str(e):\n        s = pl.Series(name, [int(v) for v in values], dtype=target_temporal_dtype)\n    else:\n        raise","preventionTips":["Convert epoch floats to int at the API boundary, not inside polars calls.","Use pl.from_epoch for unit-correct epoch ingestion.","Scale fractional seconds before truncation to avoid silent precision loss."],"tags":["datetime","float","epoch","series"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}