Load row-oriented records
Transform API, event, or configuration records into a tabular DataFrame.
Polars · Function
Constructs a Polars DataFrame from an iterable of dictionaries, treating dictionary keys as column names. Types may be inferred or controlled with schema options, and strictness governs mismatched values.
The function consumes an iterable of mappings, using dictionary keys as column names and values as row data. Polars can infer the schema from a limited number of rows, or you can specify column names and types; overrides adjust selected types, while strictness controls mismatched values.
polars.from_dicts(data: Iterable[Mapping[str, Any]], schema: SchemaDefinition | None = None, *, schema_overrides: SchemaDict | None = None, strict: bool = True, infer_schema_length: int | None = 100) → DataFrame| Name | Type | Description |
|---|---|---|
data | Iterable[Mapping[str, Any]] | Iterable of dictionaries mapping column names to values. |
schema | SchemaDefinition | None | Optional schema as a mapping, column-name sequence, or sequence of name/type pairs. |
schema_overrides | SchemaDict | None | Optional type overrides for selected columns. |
strict | bool | Whether mismatched values should raise an error or be coerced where possible. |
infer_schema_length | int | None | Maximum number of rows scanned during schema inference; defaults to 100. |
DataFrame
import polars as pl
data = [{"id": 1, "name": "Ada"}, {"id": 2, "name": "Linus"}]
df = pl.from_dicts(data, schema={"id": pl.Int32, "name": pl.String})
print(df)Creates a DataFrame from row dictionaries while explicitly setting both column types.
Transform API, event, or configuration records into a tabular DataFrame.
Guarantee stable column types across heterogeneous input records.
Process large iterables while limiting the rows inspected for type inference.
It constructs a DataFrame from a sequence of dictionaries, where dictionary keys represent columns and values represent row entries. The operation returns a DataFrame and clones the input data.
Follow-up: How would you control types during construction? Provide schema or schema_overrides; schema can be a mapping, column-name sequence, or name/type pairs.
schema defines the DataFrame schema and supports a mapping, a list of column names, or name/type pairs. A mapping value of None leaves that type to automatic inference.
Follow-up: What limits automatic inference? The infer_schema_length parameter controls the maximum number of rows scanned.
strict defaults to True and raises when values do not exactly match the given or inferred type. With strict=False, Polars attempts casting and uses null when casting cannot be performed.
Follow-up: When would strict=True be preferable? Use it when invalid input should fail immediately rather than be coerced or replaced.
polars.from_dictpolars.DataFramepolars.Schemapolars.datatypes.Struct