Skip to content

narwhals.Schema

Ordered mapping of column names to their data type.

Parameters:

Name Type Description Default
schema Mapping[str, DType] | Iterable[tuple[str, DType]] | None

Mapping[str, DType] | Iterable[tuple[str, DType]] | None The schema definition given by column names and their associated. instantiated Narwhals data type. Accepts a mapping or an iterable of tuples.

None

Examples:

Define a schema by passing instantiated data types.

>>> import narwhals as nw
>>> schema = nw.Schema({"foo": nw.Int8(), "bar": nw.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

names()

Get the column names of the schema.

dtypes()

Get the data types of the schema.

len()

Get the number of columns in the schema.