The min()
function in a Polars Series is used to calculate the minimum value in the Series. A Series in Polars is a one-dimensional array, much like a column in a DataFrame, and calling min()
on it will return the smallest value found in that Series. Similar to how min()
works in Pandas, invoking it on a Polars Series will give you the minimum value from the data.
In this article, I will explain the syntax of the Polars Series min()
function, its parameters, and how to use it to get the minimal value in this Series object.
Key Points –
- The
min()
function returns the smallest value in a Polars Series. - It works on numeric, string, and other comparable data types.
- The function ignores
None
orNaN
values when computing the minimum. - For numeric Series, it returns the minimum number, whether integer or floating-point.
- For the string Series, it returns the lexicographically smallest value.
- The
min()
function operates on a single column or Series of data. - It can be applied to a Series containing both positive and negative values.
- Similar in behavior to Pandas
Series.min()
, making it easy for Pandas users to transition.
Polars Series min() Introduction
Let’s know the syntax of the series min() function.
# Syntax of series min()
Series.min() → PythonLiteral | None
Parameters of the Polars Series min()
The following are the parameters of the polars Series min() function.
Series.min()
– This is the method you’re calling on a Polars Series.PythonLiteral | None
– This means the method will return either:- A Python literal value (like an
int
,float
,str
, etc.), or None
, if the Series is empty or all values are null.
- A Python literal value (like an
Return Value
This function returns a Python scalar (like int
, float
, str
). Returns None
if the Series is empty or all values are null.
Usage of Polars Series min() Function
The min()
function in Polars Series is used to return the smallest (minimum) value in a Polars Series. This function gets the minimum value of the given object elements in Polars.
Now, let’s create a Polars series using a list of values and use the min() function.
import polars as pl
ser = pl.Series("numbers", [10, 25, 7, 42, 18])
print("Original Series:\n", ser)
Yields below output.
You can use the min()
function to find the minimum value of integers in your Polars Series. Simply apply the min()
function to get the smallest integer from your Polars Series.
# Get the minimum value
result = ser.min()
print("Minimum value in the Series:", result)
Yields below output.
Minimum of Floats Using min() Function
To find the minimum value of floats in a Polars Series, you can use the min()
function in the same way as with integers. Simply apply the min()
function to a Series of floating-point numbers to get the minimum value.
import polars as pl
# Creating a Polars Series with float values
ser = pl.Series("floats", [10.5, 25.3, 7.8, 42.1, 18.4])
# Using min() to get the minimum value
result = ser.min()
print("Minimum float value:", result)
# Output:
# Minimum float value: 7.8
In the above example, the min()
function will return the smallest float value from the Series. In this case, the minimum value is 7.8
.
Minimum of Negative Numbers Using min() Function
To find the minimum among negative numbers in a Polars Series, you can pass a Series containing negative values and apply the min()
function as usual. When dealing with negative values, min()
returns the most negative (i.e., the smallest) number in the Series.
import polars as pl
# Creating a Polars Series with negative numbers
ser_negative = pl.Series("negatives", [-10, -25, -7, -42, -18])
# Using min() to get the minimum value
result = ser_negative.min()
print("Minimum negative value:", result)
# Output:
# Minimum negative value: -42
In the above example, the min()
function returns the most negative (smallest) value in the Series. In this case, it’s -42
.
Minimum of Strings Using min() Function
To find the minimum of strings in a Polars Series, you can apply the min()
function to a Series of string values. It returns the lexicographically smallest string, following dictionary-style ordering. The min()
function works with strings by identifying the one that comes first in alphabetical order.
import polars as pl
# Creating a Polars Series with strings
ser_strings = pl.Series("names", ["zebra", "apple", "banana", "mango", "cherry"])
# Using min() method
# To get the lexicographically smallest string
result = ser_strings.min()
print("Minimum string value:", result)
# Output:
# Minimum string value: apple
In the above example, the min()
function compares strings character by character in Unicode order, so “apple” comes before “banana”, “cherry”, etc.
Minimum with Null Values Using min() Function
When using the min()
function on a Polars Series that contains null values (represented as None
or NaN
), the function will automatically ignore those nulls and calculate the minimum value based on the non-null entries.
import polars as pl
# Creating a Polars Series with some null values
ser = pl.Series("numbers_with_nulls", [10, None, 25, None, 7])
# Using min() to get the minimum value, ignoring nulls
result = ser.min()
print("Minimum value (ignoring nulls):", result)
# Output:
# Minimum value (ignoring nulls): 7
In the above example, the min()
function disregards None
(null) values when calculating the minimum, only considering the non-null values. If all values in the Series are None
, the result will be None
, as there are no valid values to compare.
Conclusion
In conclusion, the min()
function in Polars is an effective tool for identifying the smallest value in a Series, regardless of whether the data consists of numbers, strings, or dates. It efficiently processes non-null values, ignores None
(null) entries, and returns a scalar result. Whether working with integers, floats, or strings in lexicographical order, min()
provides a simple and dependable way to retrieve the smallest value.
Happy Learning!!
Related Articles
- Convert Polars Series to List
- Polars Series max() Function
- Polars Series rename() – by Examples
- Polars Series cast() – Usage & Examples
- Polars Series tail() Usage & Examples
- Polars Series filter() Method with 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?