In Polars, the Series.median() function computes the median (middle value) of a Series after sorting its elements. It returns a Python literal (like an int or float), or None if the Series is empty or consists only of null values.
In this article, I will explain the median() method of the Polars Series, covering its syntax, parameters, and how it works. This method returns the median (middle value) of a numeric Series, and returns None if the Series is empty or contains only null values.
Key Points –
- Computes the median (middle value) of a numeric Polars Series.
- Ignores
nullvalues when calculating the median. - Returns the average of the two middle values if the Series has an even number of valid (non-null) elements.
- Returns the middle value when the number of valid elements is odd.
- Works with numeric data types like
int,float, and nullable numeric types. - Polars internally sorts the valid values before computing the median; no need to sort manually.
<a id="h-polars-series-median-introduction"></a>
Polars Series median() Introduction
Let’s know the syntax of the series median() method.
# Syntax of Series median()
Series.median() → PythonLiteral | None
<a id="h-parameters-of-the-polars-series-median"></a>
Parameters of the Polars series median()
It allows only one parameter.
None— this method takes no arguments.
<a id="h-return-value"></a>
Return Value
This function returns a Python literal (float or int) if values exist; otherwise returns None.
<a id="h-usage-of-polars-series-median-method"></a>
Usage of Polars Series median() Method
The Series.median() function in Polars returns the median of the values in a Series, that is, the central value when the data is sorted in order.
First, let’s create a Polars Series.
import polars as pl
# Sample Series with nulls
ser = pl.Series("values", [10, 20, 30, 40, 50])
print("Original Series:\n", ser)
Yields below output.
To compute the basic median of a Polars Series with an odd number of elements, you can use the median() method.
# Compute the median
ser2 = ser.median()
print("Median of the Series:", ser2)
Here,
- The values are:
[10, 20, 30, 40, 50] - Sorted order:
[10, 20, 30, 40, 50](already sorted) - Since there are 5 elements (odd), the middle value is the 3rd element,
30. So the median is30
<a id="h-basic-median-even-count"></a>
Basic Median (Even Count)
You can calculate the median of a Polars Series with an even number of elements using the median() method.
import polars as pl
# Series with an even count of elements
ser = pl.Series("values", [10, 20, 30, 40])
# Compute the median
ser2 = ser.median()
print("Median:", ser2)
# Output:
# Median: 25.0
Here,
- Sorted values:
[10, 20, 30, 40] - There’s no single middle value, so Polars takes the average of the two middle numbers: (20+30)/2=25.0
<a id="h-median-with-unsorted-values"></a>
Median with Unsorted Values
Let’s explore how Polar's handles unsorted values when computing the median using median(). Polars automatically sorts the data internally, so there’s no need to sort the values manually before calling the function.
import polars as pl
# Unsorted Series
ser = pl.Series("values", [40, 10, 30, 20])
# Compute the median
ser2 = ser.median()
print("Median:", ser2)
# Output:
# Median: 25.0
Here,
- Original (unsorted):
[40, 10, 30, 20] - Sorted:
[10, 20, 30, 40] - Even count (4 values): average of middle two (20 + 30) / 2 = 25.0
<a id="h-median-with-null-values"></a>
Median with Null Values
The median() method in Polars automatically excludes null (None) values when calculating the median. Let’s see how it works with a Series that contains nulls.
import polars as pl
# Series with null values
ser = pl.Series("values", [10, None, 30, None, 50])
# Compute the median (nulls ignored)
ser2 = ser.median()
print("Median (ignoring nulls):", ser2)
# Output:
# Median (ignoring nulls): 30.0
Here,
- Original values:
[10, None, 30, None, 50] - Polars ignores nulls when calculating the median.
- Valid values:
[10, 30, 50], Sorted:[10, 30, 50], Median =30.0
<a id="h-conclusion"></a>
Conclusion
In conclusion, the Series.median() function in Polars is a straightforward and efficient way to compute the median of numerical data. It handles unsorted values internally, automatically ignores nulls, and returns a clear result, even when working with empty or partially missing data.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series std() – Explained by Examples
- Polars Series slice() – Usage & Examples
- Polars series quantile() – Usage & Examples
- Polars abs() – Explained by Examples
- Polars Series mean() – Usage & Examples
- Polars Series min() – Explained by Examples
- Polars interpolate() Function with Examples
- Polars rolling_mean() – Usage & Examples
- Polars Series limit() – Explained by Examples
- Polars Series fill_null() Function with Examples
<a id="h-references"></a>
