Sub topic 14
Reading and scanning CSV files
This chapter assumes you know the DataFrame and schema basics and the lazy API idea from earlier chapters. It covers the two ways Polars loads a CSV file and the options that matter most.
Two entry points
pl.read_csv reads the whole file into memory and returns a DataFrame. pl.scan_csv reads nothing yet. It returns a LazyFrame, which is a query plan, and the file is read only when you call collect().
Use read_csv for small files and exploration. Use scan_csv when you will filter, aggregate, or select a few columns, because the optimizer can then skip rows and columns you never need.
A first read
The example writes a small file first, so it runs as shown.
from pathlib import Path
import polars as pl
Path("sales.csv").write_text(
"region,amount,sold_on\n"
"north,120,2026-01-05\n"
"south,-15,2026-01-06\n"
"north,80,2026-01-07\n"
"east,NA,2026-01-08\n"
)
df = pl.read_csv(
"sales.csv",
null_values=["NA"],
try_parse_dates=True,
)
print(df.schema)
print(df)Without null_values, the text NA would make amount a String column. With it, NA becomes null and amount is read as an integer. Because try_parse_dates=True, sold_on becomes a Date instead of a String.
Options you will use most
| Parameter | Purpose |
|---|---|
| `null_values` | Strings to treat as null, such as `"NA"` or `""` |
| `schema_overrides` | Force dtypes for named columns, for example `{"amount": pl.Float64}` |
| `infer_schema_length` | How many rows to inspect when guessing dtypes (default 100) |
| `try_parse_dates` | Parse date-like strings into Date or Datetime |
| `has_header` | Set to `False` if the first line is data |
| `separator` | Field delimiter, for example `";"` or `"\t"` |
read_csv also accepts columns and n_rows to load only some columns or the first rows.
Scanning and filtering
The same options work with scan_csv. Chain expressions, then collect.
import polars as pl
lazy = (
pl.scan_csv("sales.csv", null_values=["NA"], try_parse_dates=True)
.filter(pl.col("amount") > 0)
.group_by("region")
.agg(pl.col("amount").sum().alias("total"))
.sort("region")
)
print(lazy.collect())The east row has a null amount. A comparison with null gives null, and filter drops rows that are not true. The result has only north with a total of 200.
Common errors
- A numeric column turns up as String. A marker such as
NAor-appears in the data. Add it tonull_values. - A parse error on a row far down the file. Inference looked only at the first 100 rows, and a later row has a different type, such as a decimal in an integer column. Raise
infer_schema_length, or set the dtype inschema_overrides.infer_schema_length=Nonescans the whole file but costs time. - The first data row became the header. Set
has_header=False. Polars then names the columnscolumn_1,column_2, and so on. - Dates stay as strings.
try_parse_datesonly works for common formats. For others, read the column as String and convert it withstr.to_dateand an explicit format.
Exercise
A scan_csv query groups by region and sums amount, but collect() fails because a value like "1,5" appears in amount at row 5000. What are two ways to fix this?
Show answer
Inference read only the first 100 rows, so it guessed an integer dtype. Either raise inferschemalength (or set it to None), or set schemaoverrides={"amount": pl.Float64}. If the text is bad data, add the marker to nullvalues or clean the column with string expressions after reading it as String.