Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Function

pl.from_dicts

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.

How it works

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.

Syntax

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

Parameters

NameTypeDescription
dataIterable[Mapping[str, Any]]Iterable of dictionaries mapping column names to values.
schemaSchemaDefinition | NoneOptional schema as a mapping, column-name sequence, or sequence of name/type pairs.
schema_overridesSchemaDict | NoneOptional type overrides for selected columns.
strictboolWhether mismatched values should raise an error or be coerced where possible.
infer_schema_lengthint | NoneMaximum number of rows scanned during schema inference; defaults to 100.

Returns

DataFrame

Key points

  • Use the function when records arrive as an iterable of row-oriented mappings.
  • Each input dictionary maps column names to the values for one row.
  • The schema accepts multiple declaration styles, including mappings, names, and name/type pairs.
  • A schema mapping can leave an individual type as None to request inference for that column.
  • infer_schema_length bounds how many initial rows Polars examines when inferring types.
  • The default strict mode validates every value against the selected or inferred column type.

Common mistakes

  • Input data is cloned rather than retained as shared backing storage.
  • With schema inference enabled, only up to infer_schema_length rows are inspected, so later values may not influence inferred types.
  • Setting strict=False can silently produce nulls when a value cannot be cast to the selected column type.

Example: Build a typed DataFrame from row dictionaries

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.

When to use it

Load row-oriented records

Transform API, event, or configuration records into a tabular DataFrame.

Enforce a schema

Guarantee stable column types across heterogeneous input records.

Bound schema inference

Process large iterables while limiting the rows inspected for type inference.

Interview questions

QWhat does polars.from_dicts do?

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.

QHow can the schema be specified?

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.

QWhat is the effect of strict?

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_dict
  • polars.DataFrame
  • polars.Schema
  • polars.datatypes.Struct

Source: polars.from_dicts — Polars documentation. Checked 2026-10-15.