{"record":{"id":"8e3421e2c1240f13","repo":"pola-rs/polars","slug":"time-unit-must-be-one-of-ns-us-ms-s-8e3421","errorCode":null,"errorMessage":"`time_unit` must be one of {'ns', 'us', 'ms', 's', 'd'}, got {time_unit!r}","messagePattern":"`time_unit` must be one of (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/lazy.py","lineNumber":2514,"sourceCode":"        column = F.col(column)\n    elif not isinstance(column, (pl.Series, pl.Expr)):\n        column = pl.Series(column)\n\n    if time_unit == \"d\":\n        return column.cast(Date)\n    if time_unit in (scale := {\"s\": 1_000_000, \"ms\": 1_000}):\n        if isinstance(column, pl.Expr):\n            column = column * F.lit(scale[time_unit], dtype=Int64)\n            return column.cast(Datetime(\"us\"))\n        if column.dtype.is_integer():\n            column = column.cast(Int64)\n        return (column * scale[time_unit]).cast(Datetime(\"us\"))\n    if time_unit in DTYPE_TEMPORAL_UNITS:\n        return column.cast(Datetime(time_unit))  # type: ignore[arg-type]\n\n    valid_units = \"'ns', 'us', 'ms', 's', 'd'\"\n    msg = f\"`time_unit` must be one of {valid_units}, got {time_unit!r}\"\n    raise ValueError(msg)\n\n\n@removed_parameters(\n    RenamedParameter(\n        name=\"min_periods\",\n        new_name=\"min_samples\",\n        deprecated_in=\"1.21.0\",\n        removed_in=\"2.0\",\n    ),\n)\ndef rolling_cov(\n    a: str | Expr,\n    b: str | Expr,\n    *,\n    window_size: int,\n    min_samples: int | None = None,\n    ddof: int = 1,\n) -> Expr:","sourceCodeStart":2496,"sourceCodeEnd":2532,"githubUrl":"https://github.com/pola-rs/polars/blob/68506541d2de983056c9eb244e1ea05fab377dfc/py-polars/src/polars/functions/lazy.py#L2496-L2532","documentation":"ValueError raised by pl.from_epoch when time_unit is not one of the supported epoch resolutions: 'ns', 'us', 'ms', 's', or 'd'. The function needs a scale factor to convert the integer column into a Datetime, so unrecognized units (e.g. 'm' for minutes, 'us ' with whitespace, or None) are rejected.","triggerScenarios":"pl.from_epoch(col, time_unit='m'), time_unit=None, or a unit string read from config/user input without validation.","commonSituations":"Assuming minute/month granularity exists; units sourced from external metadata or user form fields; copy-paste from from_epoch docs of other libraries.","solutions":["Pick one of 'ns','us','ms','s','d'; for minutes multiply manually: (col * 60_000_000).cast(pl.Datetime('us'))","Validate/whitelist the unit before calling from_epoch","Check for typos/whitespace in dynamically built unit strings"],"exampleFix":"# before\npl.from_epoch(pl.col('ts'), time_unit='m')\n# after\n(pl.col('ts') * 60_000_000).cast(pl.Datetime('us'))","handlingStrategy":"validation","validationCode":"VALID = {'ns', 'us', 'ms', 's', 'd'}\nif time_unit not in VALID:\n    raise ValueError(f'time_unit must be one of {VALID}')","typeGuard":"def is_valid_time_unit(u: object) -> bool:\n    return u in {'ns', 'us', 'ms', 's', 'd'}","tryCatchPattern":"try:\n    pl.from_epoch(col, time_unit=u)\nexcept ValueError:\n    u = 's'\n    pl.from_epoch(col, time_unit=u)","preventionTips":["Whitelist units from user/config input","Strip and normalize unit strings before use","Remember minute/month granularity is not supported natively"],"tags":["python","polars","from-epoch","time-unit","invalid-argument"],"backgroundTag":"invalid-enum-value","analyzedSha":"68506541d2de983056c9eb244e1ea05fab377dfc","analyzedAt":"2026-08-28T18:02:35.179Z","contentChangedAt":"2026-08-28T18:02:35.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}