{"record":{"id":"8056019a4ee6a3f0","repo":"pola-rs/polars","slug":"invalid-time-unit-time-unit-r","errorCode":null,"errorMessage":"invalid time unit: {time_unit!r}","messagePattern":"invalid time unit: (.+?)","errorType":"validation","errorClass":"InvalidArgument","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/testing/parametric/strategies/data.py","lineNumber":181,"sourceCode":"    \"\"\"\n    Create a strategy for generating `datetime` objects in the time unit's range.\n\n    Parameters\n    ----------\n    time_unit\n        Time unit for which the datetime objects are valid.\n    time_zone\n        Time zone for which the datetime objects are valid.\n    \"\"\"\n    if time_unit in (\"us\", \"ms\"):\n        min_value = datetime.min\n        max_value = datetime.max\n    elif time_unit == \"ns\":\n        min_value = EPOCH + timedelta(microseconds=I64_MIN // 1000 + 1)\n        max_value = EPOCH + timedelta(microseconds=I64_MAX // 1000)\n    else:\n        msg = f\"invalid time unit: {time_unit!r}\"\n        raise InvalidArgument(msg)\n\n    if time_zone is None:\n        return st.datetimes(min_value, max_value)\n\n    time_zone_info = ZoneInfo(time_zone)\n\n    # Make sure time zone offsets do not cause out-of-bound datetimes\n    if time_unit == \"ns\":\n        min_value += timedelta(days=1)\n        max_value -= timedelta(days=1)\n\n    # Return naive datetimes, but make sure they are valid for the given time zone\n    return st.datetimes(\n        min_value=min_value,\n        max_value=max_value,\n        timezones=st.just(time_zone_info),\n        allow_imaginary=False,\n    ).map(lambda dt: dt.astimezone(timezone.utc).replace(tzinfo=None))","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/testing/parametric/strategies/data.py#L163-L199","documentation":"The datetimes() hypothesis strategy supports only 'us'/'ns' (and related) time units matching polars' temporal resolution; any other time_unit string raises InvalidArgument. The unit determines the min/max representable datetime bounds.","triggerScenarios":"datetimes(time_unit='ms') or a typo like 'micros'/'seconds' when generating strategy columns; passing a time_unit from config that isn't in the supported set.","commonSituations":"Reusing pandas/numpy unit strings ('s', 'ms') that polars doesn't accept here; confusion between polars TimeUnit variants across versions.","solutions":["Use a supported time unit string: 'us' (microseconds) or 'ns' (nanoseconds).","Map external unit strings to polars TimeUnit before calling: e.g. 'ms' -> 'us'.","Validate the unit against pl.TIME_UNITS (or the TimeUnit Literal) beforehand."],"exampleFix":"// before\ncol('ts', pl.Datetime, time_unit='ms')  # InvalidArgument\n\n// after\ncol('ts', pl.Datetime, time_unit='us')","handlingStrategy":"validation","validationCode":"VALID_UNITS = {'us', 'ns'}\nif time_unit not in VALID_UNITS:\n    raise ValueError(f\"unsupported time_unit {time_unit!r}; use one of {VALID_UNITS}\")","typeGuard":null,"tryCatchPattern":"try:\n    strat = datetimes(time_unit=time_unit)\nexcept InvalidArgument:\n    strat = datetimes(time_unit='us')","preventionTips":["Only pass polars TimeUnit strings ('us','ns') — map pandas/numpy units first","Check pl.TIME_UNITS when unsure","Centralize time-unit conversion in one utility"],"tags":["polars","parametric","datetime","invalid-argument"],"backgroundTag":"invalid-enum-value","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"}