Built with Tutify.io

    AIStudyLabs

    Polars Series min() – Explained by Examples

    Polars Series min() – Explained by Examples

    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 –

    <a id="h-polars-series-min-introduction"></a>

    Polars Series min() Introduction

    Let’s know the syntax of the series min() function.

    
    # Syntax of series min()
    Series.min() → PythonLiteral | None
    

    <a id="h-parameters-of-the-polars-series-min"></a>

    Parameters of the Polars Series min()

    The following are the parameters of the polars Series min() function.

    <a id="h-return-value"></a>

    Return Value

    This function returns a Python scalar (like int, float, str). Returns None if the Series is empty or all values are null.

    <a id="h-usage-of-polars-series-min-function"></a>

    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.

    <a id="h-minimum-of-floats-using-min-function"></a>

    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.

    <a id="h-minimum-of-negative-numbers-using-min-function"></a>

    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.

    <a id="h-minimum-of-strings-using-min-function"></a>

    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.

    <a id="h-minimum-with-null-values-using-min-function"></a>

    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.

    <a id="h-conclusion"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-reference"></a>

    Reference