Schema#

class polars.Schema(
schema: Mapping[str, DataType | PythonDataType] | Iterable[tuple[str, DataType | PythonDataType]] | None = None,
)[source]

Ordered mapping of column names to their data type.

Parameters:
schema

The schema definition given by column names and their associated instantiated Polars data type. Accepts a mapping or an iterable of tuples.

Examples

Define a schema by passing instantiated data types.

>>> schema = pl.Schema({"foo": pl.Int8(), "bar": pl.String()})
>>> schema
Schema({'foo': Int8, 'bar': String})

Access the data type associated with a specific column name.

>>> schema["foo"]
Int8

Access various schema properties using the names, dtypes, and len methods.

>>> schema.names()
['foo', 'bar']
>>> schema.dtypes()
[Int8, String]
>>> schema.len()
2

Methods:

dtypes

Get the data types of the schema.

len

Get the number of columns in the schema.

names

Get the column names of the schema.

to_python

Return Schema as a dictionary of column names and their Python types.

dtypes() list[DataType][source]

Get the data types of the schema.

len() int[source]

Get the number of columns in the schema.

names() list[str][source]

Get the column names of the schema.

to_python() dict[str, type][source]

Return Schema as a dictionary of column names and their Python types.

Examples

>>> s = pl.Schema({"x": pl.Int8(), "y": pl.String(), "z": pl.Duration("ms")})
>>> s.to_python()
{'x': <class 'int'>, 'y':  <class 'str'>, 'z': <class 'datetime.timedelta'>}