{"record":{"id":"91dc59ffc86ba705","repo":"pola-rs/polars","slug":"unit-must-be-one-of-b-kb-mb-gb-tb","errorCode":null,"errorMessage":"`unit` must be one of {'b', 'kb', 'mb', 'gb', 'tb'}, got {unit!r}","messagePattern":"`unit` must be one of (.+?), got (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/various.py","lineNumber":314,"sourceCode":"@overload\ndef scale_bytes(sz: Expr, unit: SizeUnit) -> Expr: ...\n\n\ndef scale_bytes(sz: int | Expr, unit: SizeUnit) -> int | float | Expr:\n    \"\"\"Scale size in bytes to other size units (eg: \"kb\", \"mb\", \"gb\", \"tb\").\"\"\"\n    if unit in {\"b\", \"bytes\"}:\n        return sz\n    elif unit in {\"kb\", \"kilobytes\"}:\n        return sz / 1024\n    elif unit in {\"mb\", \"megabytes\"}:\n        return sz / 1024**2\n    elif unit in {\"gb\", \"gigabytes\"}:\n        return sz / 1024**3\n    elif unit in {\"tb\", \"terabytes\"}:\n        return sz / 1024**4\n    else:\n        msg = f\"`unit` must be one of {{'b', 'kb', 'mb', 'gb', 'tb'}}, got {unit!r}\"\n        raise ValueError(msg)\n\n\ndef _cast_repr_strings_with_schema(\n    df: DataFrame, schema: dict[str, PolarsDataType | None]\n) -> DataFrame:\n    \"\"\"\n    Utility function to cast table repr/string values into frame-native types.\n\n    Parameters\n    ----------\n    df\n        Dataframe containing string-repr column data.\n    schema\n        DataFrame schema containing the desired end-state types.\n\n    Notes\n    -----\n    Table repr strings are less strict (or different) than equivalent CSV data, so need","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/_utils/various.py#L296-L332","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use one of the exact lowercase values: 'b', 'kb', 'mb', 'gb', 'tb' (or the long forms 'bytes'/'kilobytes'/...)","Normalize incoming unit strings with unit.strip().lower() before the call","Map other spellings yourself (e.g. {'kib': 'kb', 'gib': 'gb'}) before passing through"],"exampleFix":"# before\nsize = df.estimated_size(unit='GB')  # ValueError\n\n# after\nsize = df.estimated_size(unit='gb')","handlingStrategy":"validation","validationCode":"VALID_SIZE_UNITS = {'b', 'bytes', 'kb', 'kilobytes', 'mb', 'megabytes', 'gb', 'gigabytes', 'tb', 'terabytes'}\n\ndef norm_unit(unit: str) -> str:\n    u = unit.strip().lower()\n    if u not in VALID_SIZE_UNITS:\n        raise ValueError(f'unsupported unit {unit!r}; use b/kb/mb/gb/tb')\n    return u","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whitelist and lowercase unit strings from users/config before passing them on","Remember units are 1024-based and only go up to 'tb'","Separate display formatting from API arguments — never round-trip display strings into unit="],"tags":["polars","estimated-size","units","valueerror"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}