pola-rs/polars · error · ValueError

cannot concat empty list

Error message

cannot concat empty list

What it means

`pl.concat` requires at least one item to concatenate; an empty sequence has no defined result shape, so `concat` raises ValueError. This is an upfront guard before any type or strategy dispatch happens.

Solutions

  1. Check the list is non-empty before calling pl.concat
  2. Return a fallback empty DataFrame/Series instead of concatenating an empty list
  3. Fix the upstream filter/generator that yielded no items

Example fix

// before
result = pl.concat(frames)
// after
if not frames:
    raise ValueError('no frames to concat')
result = pl.concat(frames)
Defensive patterns

Strategy: validation

Validate before calling

frames = list(items)
if not frames:
    raise ValueError('no frames to concat')
result = pl.concat(frames)

Type guard

def is_concatable(items) -> bool:
    return len(items) > 0 and all(isinstance(x, (pl.DataFrame, pl.LazyFrame, pl.Series)) for x in items)

Try / catch

try:
    result = pl.concat(frames)
except ValueError as e:
    if 'empty list' in str(e):
        result = pl.DataFrame()
    else:
        raise

Prevention

When it happens

Trigger: Calling `pl.concat([])`, `pl.concat(itertools.chain())`, or passing a generator/filtered list comprehension that produced zero matching items.

Common situations: Dynamic frame lists built by filtering an empty or fully-filtered-out collection; looping over an empty dict of sources; reading zero files and concatenating resulting frames.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at py-polars/src/polars/functions/eager.py:219

    ╞═════╪══════╪══════╪═════╡
    │ 1   ┆ null ┆ null ┆ 7   │
    │ 3   ┆ null ┆ 6    ┆ 8   │
    └─────┴──────┴──────┴─────┘
    >>> pl.concat([df_a1, df_a2, df_a3], how="align_inner")
    shape: (0, 4)
    ┌─────┬─────┬─────┬─────┐
    │ id  ┆ x   ┆ y   ┆ z   │
    │ --- ┆ --- ┆ --- ┆ --- │
    │ i64 ┆ i64 ┆ i64 ┆ i64 │
    ╞═════╪═════╪═════╪═════╡
    └─────┴─────┴─────┴─────┘
    """  # noqa: W505
    # unpack/standardise (handles generator input)
    elems: Sequence[PolarsType] = list(items)

    if not elems:
        msg = "cannot concat empty list"
        raise ValueError(msg)

    if len(elems) == 1 and isinstance(
        elems[0], (pl.DataFrame, pl.Series, pl.LazyFrame)
    ):
        return elems[0]

    if how.startswith("align"):
        if not is_non_empty_sequence_of(
            elems, pl.DataFrame
        ) and not is_non_empty_sequence_of(  # type: ignore[redundant-expr]
            elems, pl.LazyFrame
        ):
            msg = f"{how!r} strategy is not supported for {qualified_type_name(elems[0])!r}"
            raise TypeError(msg)

        # establish common columns, maintaining the order in which they appear
        all_columns = list(chain.from_iterable(e.collect_schema() for e in elems))
        key = {v: k for k, v in enumerate(ordered_unique(all_columns))}

View on GitHub (pinned to fe841f959e)