pola-rs/polars · error · TypeError

Cannot pass a dictionary as a single positional…

Error message

Cannot pass a dictionary as a single positional argument.
If you merely want the *keys*, use:
  • df.method(*your_dict.keys())
If you need the key value pairs, use one of:
  • unpack as keywords:    df.method(**your_dict)
  • build expressions:     df.method(expr.alias(k) for k, expr in your_dict.items())

What it means

_parse_inputs_as_iterable rejects a single dict passed positionally, because it is ambiguous whether the caller meant keys, values, or key/expression pairs. The error message spells out the intended idioms for each intent.

Solutions

  1. Unpack the dict as keyword arguments: df.method(**your_dict).
  2. Pass only the keys: df.method(*your_dict.keys()).
  3. Build expressions from items: df.method(expr.alias(k) for k, expr in your_dict.items()).
  4. Pass a list of expressions instead: df.method([expr1, expr2]).

Example fix

// before
df.select({"total": pl.col("x").sum()})
// after
df.select(total=pl.col("x").sum())  # or df.select(**{"total": pl.col("x").sum()})
Defensive patterns

Strategy: validation

Validate before calling

def validate_expr_args(inputs):
    if len(inputs) == 1 and isinstance(inputs[0], dict):
        raise TypeError("pass dict as **kwargs or list of expressions, not a single positional dict")

Type guard

def is_single_positional_dict(args) -> bool:
    return len(args) == 1 and isinstance(args[0], dict)

Try / catch

try:
    result = df.select(arg)
except TypeError as e:
    if "dictionary as a single positional argument" in str(e):
        result = df.select(**arg)
    else:
        raise

Prevention

When it happens

Trigger: Calling expression-accepting methods like df.select({...}), df.with_columns({...}), filter with predicates chains (map_groups, _chain_predicates) with exactly one positional dict argument, e.g. df.select({"a": pl.col("x")}).

Common situations: Migrating from pandas-style rename/aggregation dicts, or from older polars versions/APIs that accepted positional dicts of named expressions.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/827040e1ddddb1bd. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/parse/expr.py:240


def _parse_inputs_as_iterable(
    inputs: tuple[Any, ...] | tuple[Iterable[Any]],
) -> Iterable[Any]:
    if not inputs:
        return []

    # Ensures that the outermost element cannot be a Dictionary (as an iterable)
    if len(inputs) == 1 and isinstance(inputs[0], Mapping):
        msg = (
            "Cannot pass a dictionary as a single positional argument.\n"
            "If you merely want the *keys*, use:\n"
            "  • df.method(*your_dict.keys())\n"
            "If you need the key value pairs, use one of:\n"
            "  • unpack as keywords:    df.method(**your_dict)\n"
            "  • build expressions:     df.method(expr.alias(k) for k, expr in your_dict.items())"
        )
        raise TypeError(msg)

    # Treat elements of a single iterable as separate inputs
    if len(inputs) == 1 and _is_iterable(inputs[0]):
        return inputs[0]

    return inputs


def _is_iterable(input: Any) -> bool:
    return isinstance(input, Iterable) and not isinstance(
        input, (str, bytes, pl.Series)
    )


def _parse_named_inputs(
    named_inputs: dict[str, IntoExpr], *, structify: bool = False
) -> Iterable[PyExpr]:
    for name, input in named_inputs.items():

View on GitHub (pinned to fe841f959e)