pola-rs/polars · error · TypeError

passing Expr objects to the DataFrame constructor is not…

Error message

passing Expr objects to the DataFrame constructor is not supported\n\nHint: Try evaluating the expression first using `select`, or if you meant to create an Object column containing expressions, pass a list of Expr objects instead.

What it means

Error "passing Expr objects to the DataFrame constructor is not supported\n\nHint: Try evaluating the expression first using `select`, or if you meant to create an Object column containing expressions, pass a list of Expr objects instead." thrown in pola-rs/polars.

Solutions

  1. Evaluate the expression first with select(), or pass a list of Expr objects to build an Object column.

Example fix

df = pl.DataFrame({'a':[1,2]}).select(pl.col('a')*2)
Defensive patterns

Strategy: validation

When it happens

Trigger: Passing a bare Expr to the DataFrame constructor.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@68506541d2 (2026-08-19). Data as JSON: /api/errors/5312dbe47bfeec5e. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/construction/dataframe.py:355

def _expand_dict_values(
    data: Mapping[str, ArrayLike | NonNestedLiteral | None],
    *,
    schema_overrides: SchemaDict | None = None,
    strict: bool = True,
    order: Sequence[str] | None = None,
    nan_to_null: bool = False,
) -> dict[str, Series]:
    """Expand any scalar values in dict data (propagate literal as array)."""
    updated_data = {}
    if data:
        if any(isinstance(val, pl.Expr) for val in data.values()):
            msg = (
                "passing Expr objects to the DataFrame constructor is not supported"
                "\n\nHint: Try evaluating the expression first using `select`,"
                " or if you meant to create an Object column containing expressions,"
                " pass a list of Expr objects instead."
            )
            raise TypeError(msg)

        dtypes = schema_overrides or {}
        data = _expand_dict_data(data, dtypes, strict=strict)
        array_len = max((arrlen(val) or 0) for val in data.values())
        if array_len > 0:
            for name, val in data.items():
                dtype = dtypes.get(name)
                if isinstance(val, dict) and dtype != Struct:
                    vdf = pl.DataFrame(val, strict=strict)
                    if (
                        vdf.height == 1
                        and array_len > 1
                        and all(not d.is_nested() for d in vdf.schema.values())
                    ):
                        s_vals = {
                            nm: vdf[nm].extend_constant(v, n=(array_len - 1))
                            for nm, v in val.items()
                        }

View on GitHub (pinned to 68506541d2)