pola-rs/polars · error · ValueError
the length of ` ` ( ) does not match the length of ` ` ( )
Error message
the length of `{value_name}` ({len(values)}) does not match the length of `{match_name}` ({n_match}) What it means
extend_bool expands a scalar bool to match a length and then validates that the resulting list length equals the number of rows/keys of the match input (by/over, etc.). If the provided list's length differs from n_match, it raises ValueError stating both lengths and the parameter names. It guards sort_by/arg_sort_by/top_k-style APIs against misaligned per-row option lists.
Solutions
- Make the list length match: descending=[True, False] for two `by` columns, or pass a scalar bool instead of a list.
- Compute lengths programmatically: descending=[True]*len(by_cols).
- Double-check that value_name and match_name in the message refer to the parameters you intended to align.
Example fix
// before df.sort(['a', 'b'], descending=[True]) // after df.sort(['a', 'b'], descending=[True, False])
Defensive patterns
Strategy: validation
Validate before calling
assert len(descending) == len(by_cols), f'descending ({len(descending)}) != by ({len(by_cols)})' Type guard
def lengths_match(a, b) -> bool:
return len(a) == len(b) Try / catch
try:
df.sort(by_cols, descending=descending)
except ValueError as e:
if 'does not match the length of' in str(e):
descending = descending * len(by_cols) if len(descending) == 1 else [False]*len(by_cols)
df = df.sort(by_cols, descending=descending) Prevention
- Pass scalar bools when one setting applies to all columns
- Derive option lists with [x]*len(by_cols)
- Re-check parallel lists after editing `by`/`over` columns
When it happens
Trigger: Calling sort/arg_sort_by/sort_by/top_k/top_k_by/bottom_k_by with by/over/descending/... option lists whose length doesn't equal the number of columns or rows expected, e.g. df.sort('a','b', descending=[True]) or df.top_k(k, by=['x','y'], reverse=[True]).
Common situations: Adding/removing a column in the `by`/`over` list without updating the parallel options list; passing multiple columns but a single-element list where a scalar would have been accepted.
Related errors
- expected at least one Series in 'coalesce' if 'eager=True'
- expected at least one Series in 'corr' inputs if…
- expected at least one Series in 'cov' inputs if 'eager=True'
- 'length' must be an integer, string, or expression, not
- list.to_struct() got a str instead of a list. hint: pass
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/f2da204314bf393f.
Report an issue: GitHub.
Appendix: source
Thrown at py-polars/src/polars/_utils/various.py:560
del stack_frame
return objects
def extend_bool(
value: bool | Sequence[bool], # noqa: FBT001
n_match: int,
value_name: str,
match_name: str,
) -> Sequence[bool]:
"""Ensure the given bool or sequence of bools is the correct length."""
values = [value] * n_match if isinstance(value, bool) else value
if n_match != len(values):
msg = (
f"the length of `{value_name}` ({len(values)}) "
f"does not match the length of `{match_name}` ({n_match})"
)
raise ValueError(msg)
return values
def in_terminal_that_supports_colour() -> bool:
"""
Determine (within reason) if we are in an interactive terminal that supports color.
Note: this is not exhaustive, but it covers a lot (most?) of the common cases.
"""
if hasattr(sys.stdout, "isatty"):
# can enhance as necessary, but this is a reasonable start
return (
sys.stdout.isatty()
and (
sys.platform != "win32"
or "ANSICON" in os.environ
or "WT_SESSION" in os.environ
or os.environ.get("TERM_PROGRAM") == "vscode"View on GitHub (pinned to fe841f959e)