pola-rs/polars · error · ValueError

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

Error message

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

What it means

ValueError from humanized_size_size (py-polars/src/polars/_utils/various.py:297-315): the `unit` argument of DataFrame/Series.estimated_size() only accepts 'b'/'bytes', 'kb'/'kilobytes', 'mb'/'megabytes', 'gb'/'gigabytes', 'tb'/'terabytes' (case-sensitive short forms plus long aliases). Any other string falls through the elif chain and raises. Units are binary (1024-based), and the accepted set is fixed at those five.

Source

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

@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 df599052da)

Solutions

  1. Use one of the exact lowercase values: 'b', 'kb', 'mb', 'gb', 'tb' (or the long forms 'bytes'/'kilobytes'/...)
  2. Normalize incoming unit strings with unit.strip().lower() before the call
  3. Map other spellings yourself (e.g. {'kib': 'kb', 'gib': 'gb'}) before passing through

Example fix

# before
size = df.estimated_size(unit='GB')  # ValueError

# after
size = df.estimated_size(unit='gb')
Defensive patterns

Strategy: validation

Validate before calling

VALID_SIZE_UNITS = {'b', 'bytes', 'kb', 'kilobytes', 'mb', 'megabytes', 'gb', 'gigabytes', 'tb', 'terabytes'}

def norm_unit(unit: str) -> str:
    u = unit.strip().lower()
    if u not in VALID_SIZE_UNITS:
        raise ValueError(f'unsupported unit {unit!r}; use b/kb/mb/gb/tb')
    return u

Prevention

When it happens

Trigger: df.estimated_size(unit='KB') (uppercase rejected), unit='kib', unit='bit', unit='pb', or a typo like 'giga'; only lowercase short/long forms listed above are valid.

Common situations: Upper-casing units for display ('GB') and passing the display string back in; copying unit strings from other libraries (e.g. humanize or psutil) that accept different spellings; user-supplied unit settings forwarded verbatim.

Related errors


AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16). Data as JSON: /api/errors/91dc59ffc86ba705. Report an issue: GitHub.