Skip to content

Tutorial

1. Load data with a schema

Polars reads a CSV file with pl.read_csv. Give it the schema of the file, as you do for any Polars program. The checker reads that schema, so trades is a typed frame from the start:

pipeline.pymypy: no errors
1import polars as pl
2
3trades = pl.read_csv(
4 "trades.csv",
5 schema={
6 "symbol": pl.String,
7 "price": pl.Float64,
8 "quantity": pl.Int64,
9 },
10)
Resulttradesdeclared
symbolStringpriceFloat64quantityInt64
Revealed type
TypedDataFrame[TypedDict({'symbol': String, 'price': Float64, 'quantity': Int64}, closed=True), None]

Nothing in this code comes from Astrolabe. The type of trades is TypedDataFrame, with three columns. Each later step is checked against them.

The next line reads a column with a typo:

pipeline.pymypy: 1 error
1import polars as pl
2
3trades = pl.read_csv(
4 "trades.csv",
5 schema={
6 "symbol": pl.String,
7 "price": pl.Float64,
8 "quantity": pl.Int64,
9 },
10)
11trades.select("symbol", "prcie")
error: Unknown column "prcie" [unknown-column]

Polars raises ColumnNotFoundError when this line runs. The checker reports it before the code runs, at the line of the typo.

If the reader gets no schema, Polars finds the data types from the data when the code runs. The checker cannot know them, so the result is the plain frame pl.DataFrame, which it does not track:

pipeline.pymypy: no errors
1import polars as pl
2
3raw = pl.read_csv("trades.csv")
4raw.select("symbol", "prcie")
Resultraw
DataFrame

The typo on prcie passes, because the checker does not know the columns of raw. Give each reader a schema with schema={...}. For a frame that comes without a schema, check it at run time with check_schema: see Check the data at run time.

Next: 2. Transform the frame.