pola-rs/polars · error · ValueError

supplying 'columns' param value is mandatory for sparklines

Error message

supplying 'columns' param value is mandatory for sparklines

What it means

Sparklines in write_excel need to know which contiguous columns of the table hold their data series. The sparkline params must include a 'columns' entry listing them; an empty or missing columns value raises ValueError.

Solutions

  1. Add 'columns' with the adjacent data columns: {'columns': ['jan','feb','mar']}
  2. Ensure the list is non-empty and contains existing DataFrame column names
  3. Use the tuple shorthand with explicit columns: {'columns': ['a','b'], 'type': 'line'}

Example fix

// before
write_excel(wb, df, sparklines={'trend': {}})
// after
write_excel(wb, df, sparklines={'trend': {'columns': ['jan', 'feb', 'mar'], 'type': 'line'}})
Defensive patterns

Strategy: validation

Validate before calling

cols = params.get('columns') if isinstance(params, dict) else params
if not cols:
    raise ValueError('sparkline params must include a non-empty columns list')

Try / catch

try:
    write_excel(wb, df, sparklines=spk)
except ValueError as e:
    if 'columns' in str(e) and 'sparkline' in str(e):
        raise  # fix config: supply sparkline 'columns'

Prevention

When it happens

Trigger: write_excel(..., sparklines={'name': {}}) or sparklines={'name': {'columns': []}} / params not a dict with usable 'columns' — i.e. data_cols resolves to empty.

Common situations: Configuring sparklines via config dicts where 'columns' was omitted or set to None/empty list; programmatically generated params with filtered-out column lists.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/io/spreadsheet/_write_utils.py:302


def _xl_inject_sparklines(
    ws: Worksheet,
    df: DataFrame,
    table_start: tuple[int, int],
    col: str,
    *,
    include_header: bool,
    params: Sequence[str] | dict[str, Any],
) -> None:
    """Inject sparklines into (previously-created) empty table columns."""
    from xlsxwriter.utility import xl_rowcol_to_cell

    m: dict[str, Any] = {}
    data_cols = params.get("columns") if isinstance(params, dict) else params
    if not data_cols:
        msg = "supplying 'columns' param value is mandatory for sparklines"
        raise ValueError(msg)
    elif not _adjacent_cols(df, data_cols, min_max=m):
        msg = "sparkline data range/cols must all be adjacent"
        raise RuntimeError(msg)

    spk_row, spk_col, _, _ = _xl_column_range(
        df, table_start, col, include_header=include_header, as_range=False
    )
    data_start_col = table_start[1] + m["min"]["idx"]
    data_end_col = table_start[1] + m["max"]["idx"]

    if not isinstance(params, dict):
        options = {}
    else:
        # strip polars-specific params before passing to xlsxwriter
        options = {
            name: val
            for name, val in params.items()
            if name not in ("columns", "insert_after", "insert_before")

View on GitHub (pinned to fe841f959e)