Every filter in Polars is an expression, not a string. That feels different at first but pays off the moment you chain conditions. Here is how to filter by number, text, null, and date.
Filter by a number
Compare the column expression directly. For a range, is_between reads cleaner than two comparisons.
- df.filter(pl.col("amount") > 100)
- df.filter(pl.col("amount").is_between(10, 50))
- df.filter(pl.col("id").is_in([1, 2, 3]))
Combine conditions
Use & and | between conditions, and wrap each one in parentheses. That parenthesis rule trips up almost everyone once.
- df.filter((pl.col("amount") > 100) & (pl.col("country") == "US"))
- df.filter((pl.col("a") < 0) | (pl.col("b").is_null()))
Filter by string (contains, like)
Polars has no LIKE keyword. Use the str namespace: contains for a substring or regex, starts_with and ends_with for anchors.
- df.filter(pl.col("name").str.contains("adil")) # substring or regex
- df.filter(pl.col("email").str.ends_with("@gmail.com"))
- df.filter(pl.col("sku").str.starts_with("AB"))
Filter nulls and use pl.lit
is_null and is_not_null handle missing data. pl.lit wraps a constant so Polars treats it as a value, not a column name, which matters inside a filter.
- df.filter(pl.col("email").is_not_null())
- df.filter(pl.col("status") == pl.lit("active"))
Filter dates, and rows that are not valid dates
Parse strings with str.to_date. To keep only the rows where a value is not a valid date, parse with strict=False so bad values become null, then filter on is_null.
- df.filter(pl.col("d").str.to_date("%Y-%m-%d") > pl.date(2026, 1, 1))
- # rows where the value is NOT a valid date:
- df.with_columns(pl.col("d").str.to_date("%Y-%m-%d", strict=False).alias("parsed")).filter(pl.col("parsed").is_null())
For creating frames to filter, see creating DataFrames; to inspect the result, see viewing DataFrames. Full reference: the Python Polars cheat sheet.
This post was originally published on my site. Read the full article and more →
Top comments (0)