{"record":{"id":"644995c06f804a16","repo":"pola-rs/polars","slug":"time-unit-must-be-one-of-valid-units-got-tim","errorCode":null,"errorMessage":"`time_unit` must be one of {valid_units}, 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":2670,"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@deprecate_renamed_parameter(\"min_periods\", \"min_samples\", version=\"1.21.0\")\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:\n    \"\"\"\n    Compute the rolling covariance between two columns/ expressions.\n\n    The window at a given row includes the row itself and the\n    `window_size - 1` elements before it.\n\n    .. versionchanged:: 1.21.0","sourceCodeStart":2652,"sourceCodeEnd":2688,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/lazy.py#L2652-L2688","documentation":"Raised by polars.from_epoch when time_unit is not one of the supported epoch units. Supported units are 'ns', 'us', 'ms', 's' and 'd'; anything else (including verbose spellings like 'microseconds' or unsupported units like 'h') reaches the final guard and raises.","triggerScenarios":"pl.from_epoch(df['ts'], time_unit='h'); time_unit='microseconds' or 'milliseconds' instead of 'us'/'ms'; a typo like time_unit='sec'; passing time_unit='M' or 'D' (pandas-style capitalization).","commonSituations":"Porting pandas or Spark code that uses different unit names; assuming ISO/pandas abbreviations; typos in ETL configuration files that feed time_unit from YAML/JSON.","solutions":["Use one of the exact short units: 'ns', 'us', 'ms', 's', or 'd'","If the value is in another unit (e.g. minutes or hours), pre-multiply the integer column yourself and then use 's' or 'ms'","Validate configuration-supplied units against the allowlist before calling"],"exampleFix":"// before\npl.from_epoch(df['ts'], time_unit='microseconds')\n// after\npl.from_epoch(df['ts'], time_unit='us')","handlingStrategy":"validation","validationCode":"VALID_EPOCH_UNITS = {'ns', 'us', 'ms', 's', 'd'}\nif time_unit not in VALID_EPOCH_UNITS:\n    raise ValueError(f'time_unit must be one of {sorted(V_VALID_EPOCH_UNITS) if False else sorted(VALID_EPOCH_UNITS)}')","typeGuard":"def is_valid_epoch_unit(u: str) -> bool:\n    return u in {'ns', 'us', 'ms', 's', 'd'}","tryCatchPattern":"try:\n    out = pl.from_epoch(col, time_unit=time_unit)\nexcept ValueError as e:\n    raise ValueError(f'bad epoch unit in config: {time_unit!r}') from e","preventionTips":["Keep an allowlist constant next to config parsing","Reject unsupported units at config load time, not deep in ETL"],"tags":["polars","datetime","from-epoch","argument-validation"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}