{"record":{"id":"118c5827e8557602","repo":"pola-rs/polars","slug":"reduce-balanced-of-empty-iterable","errorCode":null,"errorMessage":"reduce_balanced() of empty iterable","messagePattern":"reduce_balanced\\(\\) of empty iterable","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/reduce_balanced.py","lineNumber":13,"sourceCode":"from collections.abc import Callable, Iterable\nfrom typing import TypeVar\n\nT = TypeVar(\"T\")\n\n\ndef reduce_balanced(function: Callable[[T, T], T], iterable: Iterable[T]) -> T:\n    \"\"\"Applies a reduction in a balanced tree pattern.\"\"\"\n    values = list(iterable)\n\n    if not values:\n        msg = \"reduce_balanced() of empty iterable\"\n        raise TypeError(msg)\n\n    if len(values) == 1:\n        return values.pop()\n\n    stack = [(0, len(values))]\n\n    i = 0\n\n    while i < len(stack):\n        offset, length = stack[i]\n        half = -(length // -2)\n\n        if length > 3:\n            stack.append((offset + half, length - half))\n\n        if length > 2:\n            stack.append((offset, half))\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/reduce_balanced.py#L1-L31","documentation":"reduce_balanced performs a tree-shaped reduction and mirrors functools.reduce semantics, so like reduce it raises TypeError when given an empty iterable — there is no identity element to start the balanced reduction from.","triggerScenarios":"Calling pl.concat([]), pl.DataFrame(...) concatenations, union/merge_sorted with an empty list of frames, or directly calling reduce_balanced(f, []) — any empty input list routed to a balanced reduce.","commonSituations":"Building a list of DataFrames/LazyFrames in a loop that matched nothing, then concatenating the empty list.","solutions":["Check the list is non-empty before concat: if frames: pl.concat(frames).","Provide an explicit empty frame fallback, e.g. pl.concat(frames or [pl.DataFrame()]).","Guard with a sentinel: return pl.LazyFrame() when no inputs were collected.","Fix the upstream filtering/loop that produced zero items if empty is unexpected."],"exampleFix":"// before\nresult = pl.concat(frames)\n// after\nresult = pl.concat(frames) if frames else pl.DataFrame()","handlingStrategy":"validation","validationCode":"def require_non_empty(items):\n    if not items:\n        raise ValueError(\"need at least one frame to combine\")\n    return items","typeGuard":null,"tryCatchPattern":"try:\n    result = pl.concat(frames)\nexcept TypeError as e:\n    if \"empty iterable\" in str(e):\n        result = pl.DataFrame()\n    else:\n        raise","preventionTips":["Check the list length before concat/union/merge_sorted.","Provide an explicit empty-frame fallback in pipelines.","Trace why upstream collection produced zero items.","Write tests covering empty-input branches of aggregation helpers."],"tags":["polars","concat","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"}