Sub topic 11
Selecting columns and using scanners
This chapter assumes you can open a partitioned dataset with ds.dataset and build filters with ds.field. Here you will learn how column selection reduces I/O and how a scanner gives you control over reading.
Why select columns
Parquet stores each column in its own column chunk. If you ask for two columns out of twenty, PyArrow can skip the other eighteen chunks and never decode them. Partition columns are different. Their values come from directory names, so they cost almost nothing to include.
Pass columns to to_table to select by name:
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
flt = (ds.field("year") == 2024) & (ds.field("country") == "DE")
table = dataset.to_table(filter=flt, columns=["year", "country", "amount"])
print(table.to_pydict())The filter prunes directories and the projection prunes columns. Together they limit the bytes read.
What a scanner is
A scanner is an object that describes a read: which dataset, which filter, which columns. Calling to_table builds a scanner internally and runs it. Creating one yourself lets you choose how to consume the results.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
scanner = dataset.scanner(
filter=ds.field("year") >= 2023,
columns=["country", "amount"],
)
print(scanner.projected_schema)
print(scanner.count_rows())Building a scanner does not read data. Reading starts when you call a method such as to_table, to_batches or count_rows. projected_schema shows what the output will look like.
Computed columns
columns also accepts a dictionary. Keys are output names and values are expressions, so you can derive columns during the scan.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
scanner = dataset.scanner(
columns={
"country": ds.field("country"),
"amount_cents": ds.field("amount") * 100,
},
filter=ds.field("year") == 2024,
)
print(scanner.to_table().to_pydict())Consuming a scanner
| Method | Result | Use it when |
|---|---|---|
| `to_table()` | One in-memory table | The result fits in memory |
| `to_batches()` | Iterator of record batches | You want streaming |
| `head(n)` | First n rows as a table | You want a quick look |
| `count_rows()` | Integer | You only need the row count |
Common errors
- Misspelled column name. PyArrow raises an error at scanner creation, because the name is not in the dataset schema. Check
dataset.schema.names. - Filtering on a column you did not select. This is allowed. The filter column is read for evaluation and then dropped from the output.
- Reusing a consumed iterator. The iterator returned by
to_batches()is exhausted after one pass. Call it again, or create a new scanner.
Exercise
You need the total of amount for German sales in 2024, and the dataset has 30 columns. Which two arguments to dataset.scanner keep the read small, and why?
Show answer
filter and columns. The filter, for example (ds.field("year") == 2024) & (ds.field("country") == "DE"), lets PyArrow skip directories that cannot match. columns=["amount"] makes it read only the amount column chunk from the remaining files and skip the other 29.