pola-rs/polars · error · ValueError

`unit` must be one of

Error message

`unit` must be one of {{'b', 'kb', 'mb', 'gb', 'tb'}}, got {unit!r}

What it means

ValueError raised by scale_bytes's unit guard: the unit is not one of b/bytes, kb, mb, gb, tb (or their long forms), so no scaling divisor applies. It is a validation guard at the terminal else of the unit ladder, echoing the invalid unit.

Solutions

  1. Use unit one of 'b', 'kb', 'mb', 'gb', 'tb'.

Example fix

pl.Config.set_fmt_table_cell_list_len(80)  # unit='mb'
Defensive patterns

Strategy: validation

When it happens

Trigger: Invalid size unit string.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@5d8ebabf11 (2026-08-19). Data as JSON: /api/errors/97a0d16fdadece41. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/various.py:311

@overload
def scale_bytes(sz: Expr, unit: SizeUnit) -> Expr: ...


def scale_bytes(sz: int | Expr, unit: SizeUnit) -> int | float | Expr:
    """Scale size in bytes to other size units (eg: "kb", "mb", "gb", "tb")."""
    if unit in {"b", "bytes"}:
        return sz
    elif unit in {"kb", "kilobytes"}:
        return sz / 1024
    elif unit in {"mb", "megabytes"}:
        return sz / 1024**2
    elif unit in {"gb", "gigabytes"}:
        return sz / 1024**3
    elif unit in {"tb", "terabytes"}:
        return sz / 1024**4
    else:
        msg = f"`unit` must be one of {{'b', 'kb', 'mb', 'gb', 'tb'}}, got {unit!r}"
        raise ValueError(msg)


def _cast_repr_strings_with_schema(
    df: DataFrame, schema: dict[str, PolarsDataType | None]
) -> DataFrame:
    """
    Utility function to cast table repr/string values into frame-native types.

    Parameters
    ----------
    df
        Dataframe containing string-repr column data.
    schema
        DataFrame schema containing the desired end-state types.

    Notes
    -----
    Table repr strings are less strict (or different) than equivalent CSV data, so need

View on GitHub (pinned to 5d8ebabf11)