{"record":{"id":"468489a836aa09b4","repo":"pola-rs/polars","slug":"cannot-merge-sort-empty-list","errorCode":null,"errorMessage":"cannot merge_sort empty list","messagePattern":"cannot merge_sort empty list","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/eager.py","lineNumber":761,"sourceCode":"    │ megan  ┆ 33  │\n    │ ida    ┆ 37  │\n    │ steve  ┆ 42  │\n    │ elise  ┆ 44  │\n    └────────┴─────┘\n\n\n    Notes\n    -----\n    Unless ``maintain_order=True``, no guarantee is given over the output\n    row order when the key is equal between dataframes.\n\n    The key(s) must be sorted in ascending order.\n    \"\"\"\n    elems: Sequence[PolarsType] = list(items)\n\n    if not elems:\n        msg = \"cannot merge_sort empty list\"\n        raise ValueError(msg)\n    if len(elems) == 1 and isinstance(elems[0], (pl.DataFrame, pl.LazyFrame)):\n        return elems[0]\n\n    if not is_non_empty_sequence_of(\n        elems, pl.DataFrame\n    ) and not is_non_empty_sequence_of(  # type: ignore[redundant-expr]\n        elems, pl.LazyFrame\n    ):\n        msg = f\"merge_sorted is not supported for {qualified_type_name(elems[0])!r}\"\n        raise TypeError(msg)\n\n    frames = [df.lazy() for df in elems]\n\n    def reduce_fn(x: pl.LazyFrame, y: pl.LazyFrame) -> pl.LazyFrame:\n        return x.merge_sorted(y, key=key, maintain_order=maintain_order)\n\n    lf = reduce_balanced(reduce_fn, frames)\n    eager = isinstance(elems[0], pl.DataFrame)","sourceCodeStart":743,"sourceCodeEnd":779,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/eager.py#L743-L779","documentation":"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.","triggerScenarios":"pl.merge_sorted([]) or pl.merge_sorted(iterable_that_is_empty, key='...'); a list comprehension/filter that produced zero frames.","commonSituations":"Programmatically built frame lists where an upstream filter removed everything; config-driven pipelines with no sources enabled.","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"],"exampleFix":"// before\nout = pl.merge_sorted(frames, key='ts')\n// after\nif not frames:\n    return pl.DataFrame(schema=expected_schema)\nout = pl.merge_sorted(frames, key='ts')","handlingStrategy":"validation","validationCode":"if not frames:\n    return expected_empty_frame  # or raise a domain-specific error","typeGuard":null,"tryCatchPattern":"try:\n    out = pl.merge_sorted(frames, key=key)\nexcept ValueError as e:\n    if 'empty list' in str(e):\n        out = expected_empty_frame\n    else:\n        raise","preventionTips":["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"],"tags":["polars","merge-sorted","empty-input"],"backgroundTag":"empty-required-field","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}