Built with Tutify.io

    AIStudyLabs

    Polars Series abs() – Explained by Examples

    Polars Series abs() – Explained by Examples

    In Polars, the abs() method is used to compute the absolute values of all elements in a Series. This means it transforms all negative numbers into their positive equivalents, while leaving positive numbers and zeros unchanged.

    In this article, I will explore the Polars Series abs() method by detailing its syntax, parameters, and usage, and demonstrating how it generates a new Series with the absolute (non-negative) values from the original numerical Series.

    Key Points –

    <a id="h-syntax-of-polars-series-abs-method"></a>

    Syntax of Polars Series abs() Method

    Let’s know the syntax of the series abs() method.

    
    # Syntax of series abs()
    Series.abs() → Series
    

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

    Parameters of the Polars series abs()

    The following are the parameters of the polars Series abs() method

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

    Return Value

    This function return a new Series with the absolute values of each element.

    <a id="h-usage-of-polars-series-abs-method"></a>

    Usage of Polars Series abs() Method

    Series.abs() in polars generates a new Series where each element is the absolute value of the corresponding element in the original Series, converting all values to non-negative numbers.

    Now, let’s create a Polars series using a list of values and use the abs() function.

    
    import polars as pl
    
    ser = pl.Series("numbers", [-5, -2, 0, 7, 1])
    print("Original Series:\n", ser)
    

    Yields below output.

    When using abs() on a Polars Series of integers, the method returns a new Series with each integer converted to its absolute value.

    
    # Using  abs() with integers
    ser2 = ser.abs()
    print("Absolute values:\n", ser2)
    

    In the above example, It converts all negative numbers to positive, while keeping zero and positive numbers unchanged.

    <a id="h-usage-with-floating-point-numbers"></a>

    Usage with Floating-Point Numbers

    Apply abs() to a Polars Series with floating-point numbers to calculate the absolute value of each element in the Series.

    
    import polars as pl
    
    ser = pl.Series("floats", [-3.5, 0.0, 2.7, -1.1, 4.8])
    
    ser2 = ser.abs()
    print("Absolute values:\n", ser2)
    
    # Output:
    # Absolute values:
    # shape: (5,)
    # Series: 'floats' [f64]
    # [
    #  3.5
    #  0.0
    #  2.7
    #  1.1
    #  4.8
    # ]
    

    In the above example, it converts negative floats to positive, while leaving zero and positive floats as they are.

    <a id="h-series-containing-zero-values"></a>

    Series Containing Zero Values

    The abs() method operates on a Polars Series containing zeros, negative, and positive values. When applied, zero values remain unchanged while negatives are converted to their positive equivalents.

    
    import polars as pl
    
    ser = pl.Series("mixed", [-10, 0, 5, -3, 0])
    
    ser2 = ser.abs()
    print("Absolute values:\n", ser2)
    
    # Output:
    # Absolute values:
    # shape: (5,)
    # Series: 'mixed' [i64]
    # [
    #  10
    #  0
    #  5
    #  3
    #  0
    # ]
    

    In the above example, zero values remain zero, negatives become positive, and positives stay the same.

    <a id="h-handling-missing-values-null"></a>

    Handling Missing Values (Null)

    When applying abs() to a Polars Series, any missing (null) values remain unaffected and stay as nulls.

    
    import polars as pl
    
    ser = pl.Series("with_nulls", [-4, None, 3, None, -1])
    
    ser2 = ser.abs()
    print("Absolute values with nulls:\n", ser2)
    
    # Output:
    # Absolute values with nulls:
    # shape: (5,)
    # Series: 'with_nulls' [i64]
    # [
    #	4
    #	null
    #	3
    #	null
    #	1
    # ]
    

    In the above example, abs() preserves null values without altering them. Only non-null numeric values are converted to their absolute values

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

    Conclusion

    In summary, the Polars Series abs() method provides an efficient way to transform all numerical values in a Series to their absolute (non-negative) counterparts, while leaving zeros and null values unchanged. It is a useful function for data cleaning and analysis, helping ensure consistency in numerical datasets.

    Happy Learning!!

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

    Related Articles

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

    References