typestar

Reading CSV in Python

read_csv loads eagerly; scan_csv defers so the query planner can help.

from io import StringIO

import polars as pl

RAW = "day,lang,tpm\n2026-07-28,python,104\n2026-07-29,rust,98\n"

frame = pl.read_csv(StringIO(RAW), try_parse_dates=True,
                    schema_overrides={"tpm": pl.Int32})
print(frame.schema)
print(frame)

with open("runs.csv", "w") as f:
    f.write(RAW)
lazy = pl.scan_csv("runs.csv")
print(type(lazy).__name__, lazy.collect_schema().names())

How it works

  1. schema_overrides names the types you care about.
  2. try_parse_dates turns date-looking columns into real dates.
  3. scan_csv returns a LazyFrame and reads nothing yet.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
374
Tokens
104
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 62 seconds.

Type this snippet

Step 3 of 4 in Series & frames, step 3 of 32 in Polars.

← Previous Next →