Sub topic 9
Building filter expressions with ds.field
This chapter assumes you can open a dataset with ds.dataset, know that Hive partitioning puts column values in directory names, and have used a boolean mask in pandas or a WHERE clause in SQL.
What a filter expression is
A filter expression is a description of a condition, not a computed result. ds.field("year") == 2024 does not look at any data. It builds an object that the scanner evaluates later, file by file and batch by batch. Because the scanner holds the expression before it reads anything, it can use the expression to skip work.
ds.field("name") refers to a column by name. The name can be a partition column or a column stored inside the Parquet files.
Building expressions
Comparison operators produce expressions. Combine expressions with & (and), | (or) and ~ (not).
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({
"year": [2023, 2023, 2024, 2024],
"country": ["DE", "FR", "DE", "FR"],
"amount": [10.5, 20.0, 7.25, 3.0],
})
part = ds.partitioning(
pa.schema([("year", pa.int64()), ("country", pa.string())]),
flavor="hive",
)
ds.write_dataset(
table,
"sales",
format="parquet",
partitioning=part,
existing_data_behavior="overwrite_or_ignore",
)
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
year = ds.field("year")
country = ds.field("country")
amount = ds.field("amount")
flt = (year == 2024) & (country == "DE")
print(dataset.to_table(filter=flt).to_pydict())
flt = (year >= 2023) & ((country == "FR") | (amount > 10))
print(dataset.to_table(filter=flt).to_pydict())Always wrap each comparison in parentheses. In Python, & and | bind more tightly than == and >. Without parentheses, year == 2024 & country == "DE" is parsed as a chained comparison and raises an error.
Useful methods
Several common conditions have dedicated methods instead of operators.
| Condition | Expression |
|---|---|
| Value in a set | `ds.field("country").isin(["DE", "FR"])` |
| Value is null | `ds.field("amount").is_null()` |
| Value is not null | `~ds.field("amount").is_null()` |
| Range | `(amount >= 5) & (amount < 15)` |
Do not use Python's and, or or not on expressions. They try to turn the expression into a single true or false value, which raises an ArrowInvalid error.
Where filters are applied
You can pass the same expression to dataset.to_table, dataset.scanner or dataset.get_fragments. With get_fragments(filter=...), only the fragments whose partition values could match are returned. The next chapter shows how this lets the scanner skip files entirely.
Common errors
- Referring to a column name that is not in the dataset schema. The error appears when the scan runs, not when you build the expression.
- Comparing to a value of the wrong type, such as
year == "2024"when the partition field isint64. Checkdataset.schemafirst. - Forgetting the parentheses around each comparison.
Exercise
Write an expression that selects rows where country is "DE" or "FR", and year is 2023, using isin.
Show answer
(ds.field("year") == 2023) & ds.field("country").isin(["DE", "FR"])