{"record":{"id":"4b3f886cd51b3a9e","repo":"pola-rs/polars","slug":"datatypeexpr-collect-dtype-did-not-expect-context","errorCode":null,"errorMessage":"DataTypeExpr.collect_dtype did not expect {context!r}","messagePattern":"DataTypeExpr\\.collect_dtype did not expect (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatype_expr/datatype_expr.py","lineNumber":302,"sourceCode":"        ...     }\n        ... )\n        >>> pl.dtype_of(\"a\").collect_dtype(lf)\n        Int64\n        >>> pl.dtype_of(\"a\").collect_dtype({\"a\": pl.String})\n        String\n        \"\"\"\n        schema: pl.Schema\n        if isinstance(context, pl.Schema):\n            schema = context\n        elif isinstance(context, Mapping):\n            schema = pl.Schema(context)\n        elif isinstance(context, pl.DataFrame):\n            schema = context.schema\n        elif isinstance(context, pl.LazyFrame):\n            schema = context.collect_schema()\n        else:\n            msg = f\"DataTypeExpr.collect_dtype did not expect {context!r}\"\n            raise TypeError(msg)\n\n        return self._pydatatype_expr.collect_dtype(schema)\n","sourceCodeStart":284,"sourceCodeEnd":305,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatype_expr/datatype_expr.py#L284-L305","documentation":"`DataTypeExpr.collect_dtype` resolves the expression against a schema derived from the given context. Only a DataFrame's schema or a LazyFrame's collected schema is accepted; any other object (e.g. a plain Schema, dict, or Series) raises this TypeError.","triggerScenarios":"Passing something other than `pl.DataFrame` or `pl.LazyFrame` as context, e.g. `expr.collect_dtype(pl.Schema({...}))` or a Series.","commonSituations":"Developers attempt to resolve dtype expressions against a hand-built schema or a Series because collect_dtype's accepted context types are easy to misread.","solutions":["Pass a `pl.DataFrame` or `pl.LazyFrame` as the context","If you have a Schema/dict, wrap it: `expr.collect_dtype(pl.DataFrame(schema))`","If you have a Series, wrap it: `expr.collect_dtype(s.to_frame())`"],"exampleFix":"// before\nexpr.collect_dtype(pl.Schema({'a': pl.Int64}))\n// after\nexpr.collect_dtype(pl.DataFrame({'a': pl.Int64}))","handlingStrategy":"type-guard","validationCode":"def ensure_collect_context(ctx):\n    import polars as pl\n    if not isinstance(ctx, (pl.DataFrame, pl.LazyFrame)):\n        raise TypeError('collect_dtype context must be DataFrame or LazyFrame')\n    return ctx","typeGuard":"def is_collect_context(ctx) -> bool:\n    import polars as pl\n    return isinstance(ctx, (pl.DataFrame, pl.LazyFrame))","tryCatchPattern":"try:\n    dtype = expr.collect_dtype(ctx)\nexcept TypeError as e:\n    if 'did not expect' in str(e):\n        dtype = expr.collect_dtype(pl.DataFrame(ctx))\n    else:\n        raise","preventionTips":["Only pass DataFrames or LazyFrames to collect_dtype","Wrap Series/Schemas in a DataFrame first"],"tags":["polars","datatype","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}