{"record":{"id":"ffc75539ce5dd2ae","repo":"pola-rs/polars","slug":"passing-expr-objects-to-the-dataframe-constructor-is-not","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/_utils/construction/dataframe.py","lineNumber":355,"sourceCode":"def _expand_dict_values(\n    data: Mapping[str, ArrayLike | NonNestedLiteral | None],\n    *,\n    schema_overrides: SchemaDict | None = None,\n    strict: bool = True,\n    order: Sequence[str] | None = None,\n    nan_to_null: bool = False,\n) -> dict[str, Series]:\n    \"\"\"Expand any scalar values in dict data (propagate literal as array).\"\"\"\n    updated_data = {}\n    if data:\n        if any(isinstance(val, pl.Expr) for val in data.values()):\n            msg = (\n                \"passing Expr objects to the DataFrame constructor is not supported\"\n                \"\\n\\nHint: Try evaluating the expression first using `select`,\"\n                \" or if you meant to create an Object column containing expressions,\"\n                \" pass a list of Expr objects instead.\"\n            )\n            raise TypeError(msg)\n\n        dtypes = schema_overrides or {}\n        data = _expand_dict_data(data, dtypes, strict=strict)\n        array_len = max((arrlen(val) or 0) for val in data.values())\n        if array_len > 0:\n            for name, val in data.items():\n                dtype = dtypes.get(name)\n                if isinstance(val, dict) and dtype != Struct:\n                    vdf = pl.DataFrame(val, strict=strict)\n                    if (\n                        vdf.height == 1\n                        and array_len > 1\n                        and all(not d.is_nested() for d in vdf.schema.values())\n                    ):\n                        s_vals = {\n                            nm: vdf[nm].extend_constant(v, n=(array_len - 1))\n                            for nm, v in val.items()\n                        }","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/construction/dataframe.py#L337-L373","documentation":"Raised by _expand_dict_values when a dict passed to the DataFrame constructor contains pl.Expr values. Expressions are lazy and unevaluated, so they cannot become column data; Polars raises TypeError with a hint to evaluate the expression first or to explicitly wrap it in a list for an Object column.","triggerScenarios":"pl.DataFrame({'a': pl.col('x') * 2}) — passing an Expr directly as a dict value to pl.DataFrame.__init__ or dict_to_pydf.","commonSituations":"Confusing the expression context (df.select / with_columns) with the constructor, translating pandas code where such a value would have been a Series, building column definitions dynamically and accidentally using expressions.","solutions":["Evaluate the expression first, e.g. df.select(expr) and then build the DataFrame from the result","Convert the underlying data to a Series/list before constructing","If an Object column of expressions is truly intended, pass a list: {'a': [pl.col('x')]}"],"exampleFix":"// before\ndf = pl.DataFrame({'double_x': pl.col('x') * 2})\n// after\ndf = df.select((pl.col('x') * 2).alias('double_x'))","handlingStrategy":"type-guard","validationCode":"if isinstance(data, dict):\n    exprs = [v for v in data.values() if isinstance(v, pl.Expr)]\n    assert not exprs, f'Expr values cannot be column data: {exprs}'","typeGuard":"def is_expr_dict(data: dict) -> bool:\n    return any(isinstance(v, pl.Expr) for v in data.values())","tryCatchPattern":"try:\n    df = pl.DataFrame(data)\nexcept TypeError as e:\n    if 'passing Expr objects' in str(e):\n        raise TypeError('evaluate expressions with select/with_columns first') from e\n    raise","preventionTips":["Only build DataFrames from evaluated data (lists, Series, arrays)","Use df.select/with_columns for expression logic","Watch for accidental Expr leakage when generating column specs dynamically"],"tags":["python","polars","expressions","lazy-evaluation"],"backgroundTag":"invalid-constructor-argument","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"}