In Polars, the Series.filter() function is used to retrieve specific values from a Series based on a Boolean condition (also known as a mask). It returns a new Series containing only the elements where the condition is True.
In this article, I will explain the Polars Series filter() method by using its syntax, parameters, and usage to demonstrate how it returns a new Series that contains only the values where the Boolean condition is True.
Key Points –
- The
filter()method selects values from a Series based on a Boolean condition or mask. - It returns a new Series containing only the elements where the condition evaluates to
True. - The condition must be either a Polars Series of Boolean values or a Python iterable of booleans.
- The length of the Boolean mask must match the length of the Series.
- The method supports filtering using manually created Boolean lists.
- You can combine multiple conditions using logical operators like
&(and),|(or), and~(not). - Common usage involves passing a condition like (
series > 10) directly into the filter. - The input to
filter()must be a Boolean Series or list of the same length as the original Series.
Polars Series filter() Introduction
Let’s know the syntax of the series filter() function.
# Syntax of polars Series filter()
Series.filter(predicate: Series | Iterable[bool]) → Self
Parameters of the Polars Series filter()
The following are the parameters of the polars Series filter() method.
Series.filter(…)– This is a method available on a Polars Series.predicate: Series | Iterable[bool]– This means the argumentpredicatecan be either:- Another Polars Series of Boolean values, or
- Any iterable of Boolean values (like a list of True/False)
Return Value
This function returns a new Series, containing only the elements where the corresponding predicate value is True.
Usage of Polars Series filter() Function
The Series.filter() method is used to select specific values from a Polars Series based on a Boolean condition (or Boolean mask). It returns a new Series containing only the elements where the mask is True.
Now, let’s create a Polars Series.
import polars as pl
ser = pl.Series("numbers", [10, 15, 20, 25, 30])
print("Original Series:\n", ser)
Yields below output.
To filter values greater than a number in a Polars Series, you can use the filter() function along with a Boolean condition.
# Filter values greater than 20
result = ser.filter(ser > 20)
print("Filtered Series (values > 20):\n", result)
Here,
ser > 20creates a Boolean mask:[False, False, False, True, True]ser.filter(…)keeps only the values where the mask isTrue.
Filter Even Numbers
To filter even numbers from a Polars Series, you can use the modulo operator % to check if a number is divisible by 2 (i.e., number % 2 == 0).
# Filter even numbers
result = ser.filter(ser % 2 == 0)
print("Even Numbers:\n", result)
# Output:
# Even Numbers:
# shape: (3,)
# Series: 'numbers' [i64]
# [
# 10
# 20
# 30
# ]
Here,
ser % 2computes the remainder when each element is divided by 2.ser % 2 == 0creates a Boolean mask identifying even numbers.== 0checks whether the remainder is 0, meaning the number is even.- The
filter()method keeps values where the mask isTrue.
Filter Values Less Than or Equal to 15
To filter values less than or equal to 15 in a Polars Series, you can use the <= operator to create a boolean mask, and then use filter() to get the matching values.
# Filter values <= 15
result = ser.filter(ser <= 15)
print("Filtered Series (values <= 15):\n", result)
# Output:
# Filtered Series (values <= 15):
# shape: (2,)
# Series: 'numbers' [i64]
# [
# 10
# 15
# ]
Here,
ser <= 15creates a Boolean mask:[True, True, False, False, False].filter()keeps only the values where the mask isTrue.
Filter Values That are Multiples of 5 explain
To filter values that are multiples of 5 in a Polars Series, you can use the modulo operator (%) to check if the remainder when dividing by 5 is zero.
# Filter values that are multiples of 5
result = ser.filter(ser % 5 == 0)
print("Multiples of 5:\n", result)
# Output:
# Multiples of 5:
# shape: (5,)
# Series: 'numbers' [i64]
# [
# 10
# 15
# 20
# 25
# 30
# ]
Here,
ser % 5 == 0creates a Boolean mask to find numbers divisible by 5.filter()keeps only those values.
Filter Using a Manual Boolean Mask (Python List)
You can manually filter a Polars Series using a Boolean mask provided as a plain Python list of True/False values.
# Define a manual Boolean mask (must be the same length as the Series)
mask = [True, False, True, False, True]
# Apply the filter
result = ser.filter(mask)
print("Filtered Series (manual mask):\n", result)
# Output:
# Filtered Series (manual mask):
# shape: (3,)
# Series: 'numbers' [i64]
# [
# 10
# 20
# 30
# ]
Here,
- The mask
[True, False, True, False, True]selects the 1st, 3rd, and 5th elements. - Polars applies the mask directly to filter the Series.
Filter Using a Condition with Logical AND
Filter a Polars Series using the logical AND operator (&). This means selecting values that meet all specified conditions simultaneously.
# Filter values greater than 10 AND less than 30
result = ser.filter((ser > 10) & (ser < 30))
print("Filtered Series (10 < values < 30):\n", result)
# Output:
# Filtered Series (10 < values < 30):
# shape: (3,)
# Series: 'numbers' [i64]
# [
# 15
# 20
# 25
# ]
Here,
(ser > 10)→[False, True, True, True, True](ser < 30)→[True, True, True, True, False]- Combining with
&gives:[False, True, True, True, False] filter()keeps the values where the final mask isTrue.
Filter Using a Condition with Logical OR
Filter a Polars Series using a condition with the logical OR operator (|). This allows you to select values that meet at least one of multiple conditions.
# Filter values less than 15 OR greater than 25
result= ser.filter((ser < 15) | (ser > 25))
print("Filtered Series (values < 15 or > 25):\n", result)
# Output:
# Filtered Series (values < 15 or > 25):
# shape: (2,)
# Series: 'numbers' [i64]
# [
# 10
# 30
# ]
Here,
(ser < 15)→[True, False, False, False, False](ser > 25)→[False, False, False, False, True]- Combined with
|→[True, False, False, False, True] filter()returns only those values.
Conclusion
In conclusion, the Series.filter() method in Polars is a powerful and efficient way to extract specific values based on conditions. By using Boolean masks, you can easily select elements that meet specific criteria, making your data processing tasks both efficient and expressive.
Happy Learning!!
Related Articles
- Polars Filter by Column Value
- Polars DataFrame.filter() Usage & Examples
- Polars Filter DataFrame with Multilple Conditions
- Polars Series sort() Usage & Examples
- Polars Series rename() – by Examples
- Polars Series cast() – Usage & Examples
- Polars Series head() Method with Examples
- Polars Series min() – Explained by Examples
- How to Convert Struct to Series in Polars?
- How to Replace Certain Values in a Polars Series?
- How to Transform a Series of a Polars DataFrame?
- How to Convert Polars Series to Pandas Series in Python?