pola-rs/polars · error · InvalidArgument

invalid time unit: {time_unit!r}

Error message

invalid time unit: {time_unit!r}

What it means

Branch failure in the datetime strategy of the parametric test framework: the requested time_unit for generating datetime objects is neither 'us', 'ms', nor 'ns', so the representable min/max range for the strategy cannot be selected.

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 68506541d2)

Solutions

  1. Use a valid time unit: one of 'ms', 'us', or 'ns'.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at py-polars/src/polars/testing/parametric/strategies/data.py:179 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19). Data as JSON: /api/errors/8056019a4ee6a3f0. Report an issue: GitHub.