Membership filtering
Filter or flag rows whose scalar values belong to an allowed-value collection.
Polars · Module
Checks whether each value in a Series occurs in another Series or collection. It returns a Boolean Series, with optional control over whether null is treated as a distinct value.
Series.is_in compares each value with a Series or collection and returns a Boolean Series indicating membership. By default, null follows null-propagation semantics; setting nulls_equal=True makes null a distinct comparable value.
Series.is_in(*other: Series | Collection[Any], ***, nulls_equal: bool = False) → Series| Name | Type | Description |
|---|---|---|
other | Series | Collection[Any] | A Series or collection to search in. |
nulls_equal | bool, default False | If True, treat null as a distinct value. Null values will not propagate. |
Series of data type Boolean.
import polars as pl
values = pl.Series("values", [1, 2, 3, None])
allowed = [2, 3]
print(values.is_in(allowed))
print(values.is_in(allowed, nulls_equal=True))Creates Boolean membership masks, with the second call treating null as a distinct value.
Filter or flag rows whose scalar values belong to an allowed-value collection.
Test candidate values against list-valued Series elements.
Choose explicit null behavior when building Boolean masks.
Series.is_in checks whether each element of the Series occurs in the supplied Series or collection and returns a Boolean Series. Its signature accepts the search values through other and exposes nulls_equal as a keyword-only option.
Follow-up: How would you make null comparable during membership testing? Pass nulls_equal=True so null is treated as a distinct value.
By default, nulls_equal is False, so null participates in null-propagation behavior. When it is True, null is considered a distinct value for the membership check.
Follow-up: What changes in the result when the searched values contain null? A null input can produce a Boolean result when nulls_equal=True, depending on whether null is present in the search values.
The method accepts either another Series or a collection as the values to search. The resulting Series has Boolean data type.
Follow-up: How can membership be tested against row-wise list values? Use the Series containing candidate values as the receiver and the Series of list values as other, as shown by the documented sublist example.
polars.Expr.is_inpolars.Expr.list.containspolars.Expr.arr.contains