pola-rs/polars · error · TypeError
reduce_balanced() of empty iterable
Error message
reduce_balanced() of empty iterable
What it means
TypeError raised by reduce_balanced: the input iterable was empty, so there is no initial value to seed the balanced-tree reduction. It mirrors Python's built-in reduce behavior of refusing to reduce an empty sequence without an initializer.
Source
Thrown at py-polars/src/polars/_utils/reduce_balanced.py:13
from collections.abc import Callable, Iterable
from typing import TypeVar
T = TypeVar("T")
def reduce_balanced(function: Callable[[T, T], T], iterable: Iterable[T]) -> T:
"""Applies a reduction in a balanced tree pattern."""
values = list(iterable)
if not values:
msg = "reduce_balanced() of empty iterable"
raise TypeError(msg)
if len(values) == 1:
return values.pop()
stack = [(0, len(values))]
i = 0
while i < len(stack):
offset, length = stack[i]
half = -(length // -2)
if length > 3:
stack.append((offset + half, length - half))
if length > 2:
stack.append((offset, half))
View on GitHub (pinned to 9b5d73fd00)
Solutions
- Pass a non-empty iterable to reduce_balanced, or handle the empty case before calling.
Example fix
if items: reduce_balanced(items)
Defensive patterns
Strategy: validation
When it happens
Trigger: reduce_balanced called with an empty iterable.
Common situations: See trigger scenarios.
AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19).
Data as JSON: /api/errors/118c5827e8557602.
Report an issue: GitHub.