typestar

Writing frames out in Python

Parquet keeps the types; CSV keeps the readers; sink streams to disk.

import polars as pl

frame = pl.DataFrame({
    "lang": ["python", "rust"],
    "tpm": [104.5, 98.0],
    "stars": [3, 3],
})

frame.write_parquet("runs.parquet")
frame.write_csv("runs.csv")
frame.write_ndjson("runs.ndjson")

back = pl.read_parquet("runs.parquet")
print(back.schema == frame.schema, back.height)
print(pl.read_csv("runs.csv").schema)

frame.lazy().filter(pl.col("stars") == 3).sink_parquet("best.parquet")
print(pl.read_parquet("best.parquet").height)

How it works

  1. write_parquet round-trips the schema exactly.
  2. write_csv loses the types but anything can read it.
  3. sink_parquet writes from a LazyFrame without loading it all.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
456
Tokens
147
Three-star pace
110 tpm

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

Type this snippet

Step 1 of 2 in Reading & writing, step 29 of 32 in Polars.

← Previous Next →