pola-rs/polars · error

number of characters must be > 0

Error message

number of characters must be > 0

What it means

Value validation in Config.set_fmt_str_lengths: the setter requires a positive integer for the maximum string length shown in formatted output; passing 0 or a negative value violates the invariant that at least one character must be displayed.

Solutions

  1. Pass a positive integer, e.g. set_fmt_str_lengths(50)
  2. Pass None to reset to the default
  3. Guard with max(1, n) when computing dynamically

Example fix

// before
pl.Config.set_fmt_str_lengths(len(df))  # 0 -> ValueError
// after
pl.Config.set_fmt_str_lengths(max(1, len(df)))
Defensive patterns

Strategy: validation

Validate before calling

n = 50  # computed
assert n is None or n > 0, f'n must be > 0, got {n}'

Type guard

def valid_str_len(n: int | None) -> bool:
    return n is None or n > 0

Try / catch

try:
    pl.Config.set_fmt_str_lengths(n)
except ValueError:
    pl.Config.set_fmt_str_lengths(None)  # fall back to default

Prevention

When it happens

Trigger: pl.Config.set_fmt_str_lengths(0) or set_fmt_str_lengths(-5); passing None resets the env var and is allowed.

Common situations: Computing the length dynamically and getting 0/negative; misreading the API as 'extra padding' rather than an absolute count.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/ab7839af84555221. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/config.py:883

        └─────────────────────────────────┴─────┘
        >>> with pl.Config(fmt_str_lengths=50):
        ...     print(df)
        shape: (2, 1)
        ┌──────────────────────────────────────────────────┐
        │ txt                                              │
        │ ---                                              │
        │ str                                              │
        ╞══════════════════════════════════════════════════╡
        │ Play it, Sam. Play 'As Time Goes By'.            │
        │ This is the beginning of a beautiful friendship. │
        └──────────────────────────────────────────────────┘
        """
        if n is None:
            os.environ.pop("POLARS_FMT_STR_LEN", None)
        else:
            if n <= 0:
                msg = "number of characters must be > 0"
                raise ValueError(msg)

            os.environ["POLARS_FMT_STR_LEN"] = str(n)
        plr.config_reload_env_var("POLARS_FMT_STR_LEN")
        return cls

    @classmethod
    def set_fmt_table_cell_list_len(cls, n: int | None) -> type[Config]:
        """
        Set the number of elements to display for List values.

        Empty lists will always print "[]". Negative values will result in all values
        being printed. A value of 0 will always "[…]" for lists with contents. A value
        of 1 will print only the final item in the list.

        Parameters
        ----------
        n : int
            Number of values to display.

View on GitHub (pinned to fe841f959e)