pola-rs/polars · error · TypeError
merge_sorted is not supported for
Error message
merge_sorted is not supported for {qualified_type_name(elems[0])!r} What it means
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.
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
Example fix
// before pl.merge_sorted([df, s1], key='ts') // after pl.merge_sorted([df, s1.to_frame()], key='ts')
Defensive patterns
Strategy: type-guard
Validate before calling
assert frames and all(isinstance(f, (pl.DataFrame, pl.LazyFrame)) for f in frames), 'merge_sorted needs DataFrame/LazyFrame items'
Type guard
def is_frame_list(items) -> bool:
return bool(items) and all(isinstance(i, (pl.DataFrame, pl.LazyFrame)) for i in items) Try / catch
try:
out = pl.merge_sorted(items, key=key)
except TypeError as e:
logger.error('merge_sorted input type problem: %s', e)
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: Building a heterogeneous list of frames; confusing Series concat with frame merge_sorted; forgetting to convert a pandas/arrow object.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- arr.to_struct() got a str instead of a list. hint: pass
- Array constructor is missing the required argument `shape`
- cannot call `map_groups` when filtering groups with `having`
- cannot call `map_groups` when grouping by an expression
- cannot call `map_groups` when grouping by named expressions
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/ab9a844063108596.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/functions/eager.py:771
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)
return lf._collect_eager() if eager else lf # type: ignore[return-value]
def _alignment_join(
*idx_frames: tuple[int, LazyFrame],
align_on: list[str],
how: JoinStrategy = "full",
descending: bool | Sequence[bool] = False,
eager: bool = False,
) -> LazyFrame:View on GitHub (pinned to fe841f959e)