pola-rs/polars · error · InvalidArgument
invalid time unit
Error message
invalid time unit: {time_unit!r} What it means
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.
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.
Example fix
// before
col('ts', pl.Datetime, time_unit='ms') # InvalidArgument
// after
col('ts', pl.Datetime, time_unit='us') Defensive patterns
Strategy: validation
Validate before calling
VALID_UNITS = {'us', 'ns'}
if time_unit not in VALID_UNITS:
raise ValueError(f"unsupported time_unit {time_unit!r}; use one of {VALID_UNITS}") Try / catch
try:
strat = datetimes(time_unit=time_unit)
except InvalidArgument:
strat = datetimes(time_unit='us') Prevention
- 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
When it happens
Trigger: 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.
Common situations: Reusing pandas/numpy unit strings ('s', 'ms') that polars doesn't accept here; confusion between polars TimeUnit variants across versions.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- activate feature dtype-datetime
- cannot create datetime from other type. dtype
- cannot use credential_provider when passing a DeltaTable…
- comparing datetimes with different units or timezones is…
- DataTypeExpr.collect_dtype did not expect
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/8056019a4ee6a3f0.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/testing/parametric/strategies/data.py:181
"""
Create a strategy for generating `datetime` objects in the time unit's range.
Parameters
----------
time_unit
Time unit for which the datetime objects are valid.
time_zone
Time zone for which the datetime objects are valid.
"""
if time_unit in ("us", "ms"):
min_value = datetime.min
max_value = datetime.max
elif time_unit == "ns":
min_value = EPOCH + timedelta(microseconds=I64_MIN // 1000 + 1)
max_value = EPOCH + timedelta(microseconds=I64_MAX // 1000)
else:
msg = f"invalid time unit: {time_unit!r}"
raise InvalidArgument(msg)
if time_zone is None:
return st.datetimes(min_value, max_value)
time_zone_info = ZoneInfo(time_zone)
# Make sure time zone offsets do not cause out-of-bound datetimes
if time_unit == "ns":
min_value += timedelta(days=1)
max_value -= timedelta(days=1)
# Return naive datetimes, but make sure they are valid for the given time zone
return st.datetimes(
min_value=min_value,
max_value=max_value,
timezones=st.just(time_zone_info),
allow_imaginary=False,
).map(lambda dt: dt.astimezone(timezone.utc).replace(tzinfo=None))View on GitHub (pinned to fe841f959e)