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
write_parquetround-trips the schema exactly.write_csvloses the types but anything can read it.sink_parquetwrites from a LazyFrame without loading it all.
Keywords and builtins used here
asprint
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.
Step 1 of 2 in Reading & writing, step 29 of 32 in Polars.