Sub topic 8
Sinking results straight to disk
This chapter assumes you know basic Polars expressions and have seen scan_csv, scan_parquet, and collect(engine="streaming") from earlier chapters.
The problem with collecting
collect(engine="streaming") reads the input in chunks, but it still returns one DataFrame. If the result is larger than memory, the query fails at the last step. This happens often in cleaning jobs, where a 200 GB input becomes a 150 GB output.
A sink avoids this. It runs the lazy query and writes each chunk of output to a file as it is produced. No full result is ever held in memory.
Your first sink
This query keeps rows with a positive amount and adds an integer column. The output is written to a new Parquet file.
import polars as pl
lf = (
pl.scan_csv("events.csv")
.filter(pl.col("amount") > 0)
.with_columns((pl.col("amount") * 100).cast(pl.Int64).alias("amount_cents"))
)
lf.sink_parquet("events_clean.parquet")There is no collect() call. sink_parquet triggers execution itself, and it returns None because the data went to disk.
Other sinks
Polars has a sink method for each common output format.
| Method | Output | Typical use |
|---|---|---|
| `sink_parquet` | Parquet | Default choice. Compact, typed, and fast to scan again. |
| `sink_ipc` | Arrow IPC (Feather) | Fast hand-off to other Arrow tools. |
| `sink_csv` | CSV | Exchange with tools that only read text. |
| `sink_ndjson` | Newline-delimited JSON | Log-style pipelines and APIs. |
Prefer Parquet for intermediate results. Because it stores types and compresses well, the next step can scan it with pushdown instead of parsing text again.
Checking the result
Verify the output by scanning it, not by reading it. A scan with an aggregation costs almost no memory.
import polars as pl
check = pl.scan_parquet("events_clean.parquet").select(pl.len().alias("rows"))
print(check.collect())Things to watch
- Row order. The streaming engine processes chunks in parallel. By default Polars tries to keep the original row order, which can cost some speed. If your output order does not matter,
sink_parquetacceptsmaintain_order=False. Check your Polars version's documentation for the exact parameters. - Memory-heavy steps still apply. A sink removes the final result as a memory cost. A
group_by,join, orsortearlier in the query can still need a lot of memory. Check the plan withexplain()before a long run. - One query, one sink. A sink consumes the whole lazy query. If you need two outputs from the same scan, you either run two queries or write one intermediate file and scan it twice.
- Overwriting. Writing to an existing path replaces that file. Write to a new path first if the job might fail partway, so you do not lose good data.
When to use collect and when to sink
Use collect(engine="streaming") when the result is small, for example after a heavy aggregation, and you want to keep working in memory. Use a sink when the result is large, or when the next step reads from disk anyway.
Exercise
A lazy query filters a 300 GB CSV and keeps about half of the rows. Why is .collect(engine="streaming") a risky way to run it, and what should you call instead?
Show answer
Streaming only keeps the input out of memory. collect still builds the whole result as one DataFrame, and roughly 150 GB will not fit in RAM. Call a sink such as sink_parquet("out.parquet"), which writes each chunk to disk as it is produced.
Exercise
After lf.sink_parquet("events_clean.parquet") finishes, how can you count the rows in the file without loading it all into memory?
Show answer
Use pl.scan_parquet("events_clean.parquet").select(pl.len()).collect(). The scan is lazy and the aggregation returns a single row.