{"record":{"id":"ab9a844063108596","repo":"pola-rs/polars","slug":"merge-sorted-is-not-supported-for-qualified-type","errorCode":null,"errorMessage":"merge_sorted is not supported for {qualified_type_name(elems[0])!r}","messagePattern":"merge_sorted is not supported for (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/eager.py","lineNumber":771,"sourceCode":"    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)\n    return lf._collect_eager() if eager else lf  # type: ignore[return-value]\n\n\ndef _alignment_join(\n    *idx_frames: tuple[int, LazyFrame],\n    align_on: list[str],\n    how: JoinStrategy = \"full\",\n    descending: bool | Sequence[bool] = False,\n    eager: bool = False,\n) -> LazyFrame:","sourceCodeStart":753,"sourceCodeEnd":789,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/eager.py#L753-L789","documentation":"Type validation in merge_sorted: every element must be a DataFrame or LazyFrame; if the first element is some other type (Series, plain list, etc.), the function cannot merge-sort it and raises naming its type.","triggerScenarios":"pl.merge_sorted([series_or_other], key='k'); passing a mix like [df, lf] or [df, series] so the is_non_empty_sequence_of checks both fail; passing a non-polars frame.","commonSituations":"Building a heterogeneous list of frames; confusing Series concat with frame merge_sorted; forgetting to convert a pandas/arrow object.","solutions":["Ensure all items are pl.DataFrame or pl.LazyFrame (convert with pl.DataFrame(...) or .lazy())","Homogenize the list to a single frame type before calling","For Series use pl.concat instead","Convert external frames via pl.from_pandas/pl.from_arrow"],"exampleFix":"// before\npl.merge_sorted([df, s1], key='ts')\n// after\npl.merge_sorted([df, s1.to_frame()], key='ts')","handlingStrategy":"type-guard","validationCode":"assert frames and all(isinstance(f, (pl.DataFrame, pl.LazyFrame)) for f in frames), 'merge_sorted needs DataFrame/LazyFrame items'","typeGuard":"def is_frame_list(items) -> bool:\n    return bool(items) and all(isinstance(i, (pl.DataFrame, pl.LazyFrame)) for i in items)","tryCatchPattern":"try:\n    out = pl.merge_sorted(items, key=key)\nexcept TypeError as e:\n    logger.error('merge_sorted input type problem: %s', e)\n    raise","preventionTips":["Homogenize items to pl.DataFrame/pl.LazyFrame before merging","Convert Series via .to_frame() when a frame is required","Convert pandas/arrow objects with pl.from_* helpers at boundaries"],"tags":["polars","merge-sorted","type-error"],"backgroundTag":"type-mismatch","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"}