pola-rs/polars · error · ValueError
cannot merge_sort empty list
Error message
cannot merge_sort empty list
What it means
Guard in merge_sorted (pl.concat with how='merge_sorted' style helper): the items sequence is empty after list conversion, and a merge sort needs at least one input frame to produce a result, so it raises ValueError.
Solutions
- Check the list is non-empty before calling merge_sorted
- Short-circuit: if len(frames) == 0 return an empty frame of the expected schema
- Guard the reduction with functools.reduce(frames, merge_sorted) only when len >= 2
- Fix the upstream filtering that produced an empty collection
Example fix
// before
out = pl.merge_sorted(frames, key='ts')
// after
if not frames:
return pl.DataFrame(schema=expected_schema)
out = pl.merge_sorted(frames, key='ts') Defensive patterns
Strategy: validation
Validate before calling
if not frames:
return expected_empty_frame # or raise a domain-specific error Try / catch
try:
out = pl.merge_sorted(frames, key=key)
except ValueError as e:
if 'empty list' in str(e):
out = expected_empty_frame
else:
raise Prevention
- Check collections are non-empty right before merge operations
- Return an empty frame of known schema for degenerate inputs
- Log when upstream filters eliminate all frames
When it happens
Trigger: pl.merge_sorted([]) or pl.merge_sorted(iterable_that_is_empty, key='...'); a list comprehension/filter that produced zero frames.
Common situations: Programmatically built frame lists where an upstream filter removed everything; config-driven pipelines with no sources enabled.
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
- cannot concat empty list
- merge_sorted is not supported for
- no data, cannot infer schema
- reduce_balanced() of empty iterable
- {0}
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/468489a836aa09b4.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/functions/eager.py:761
│ megan ┆ 33 │
│ ida ┆ 37 │
│ steve ┆ 42 │
│ elise ┆ 44 │
└────────┴─────┘
Notes
-----
Unless ``maintain_order=True``, no guarantee is given over the output
row order when the key is equal between dataframes.
The key(s) must be sorted in ascending order.
"""
elems: Sequence[PolarsType] = list(items)
if not elems:
msg = "cannot merge_sort empty list"
raise ValueError(msg)
if len(elems) == 1 and isinstance(elems[0], (pl.DataFrame, pl.LazyFrame)):
return elems[0]
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"merge_sorted is not supported for {qualified_type_name(elems[0])!r}"
raise TypeError(msg)
frames = [df.lazy() for df in elems]
def reduce_fn(x: pl.LazyFrame, y: pl.LazyFrame) -> pl.LazyFrame:
return x.merge_sorted(y, key=key, maintain_order=maintain_order)
lf = reduce_balanced(reduce_fn, frames)
eager = isinstance(elems[0], pl.DataFrame)View on GitHub (pinned to fe841f959e)