Built with Tutify.io

    AIStudyLabs

    Extract Value of Polars Literal

    Extract Value of Polars Literal

    In Polars, a literal (pl.lit()) represents a constant value as an expression. You can extract its value by evaluating it within an expression. This function creates a Polars expression rather than a direct Python value. However, sometimes we need to extract the actual Python value from this expression. In this article, I will demonstrate how to extract the value of a polars literal.

    Key Points–

    <a id="h-usage-of-extract-value-of-polars-literal"></a>

    Usage of Extract Value of Polars Literal

    A literal (pl.lit()) represents a constant value as a Polars expression rather than a direct Python value. To obtain the actual Python value from this expression, you need to evaluate it within an expression.

    <a id="h-extracting-an-integer-literal"></a>

    Extracting an Integer Literal

    When you create an integer literal using pl.lit(), it is treated as a Polars expression rather than a direct Python integer. To extract the actual integer value, you need to evaluate it within a DataFrame and retrieve the result.

    
    import polars as pl
    
    # Define an integer literal
    literal_expr = pl.lit(42)
    
    # Extract the value by evaluating it in a DataFrame
    result = pl.select(literal_expr).item()
    print("Extracting an integer literal:", result)
    

    Here,

    <a id="h-extracting-a-float-literal"></a>

    Extracting a Float Literal

    When you create a float literal with pl.lit(), it is recognized as a Polars expression instead of a direct Python float. To obtain the actual float value, you must evaluate it within a DataFrame and extract the result.

    
    import polars as pl
    
    # Define a float literal
    literal_expr = pl.lit(3.14159)
    
    # Extract the value by evaluating it in a DataFrame
    result = pl.DataFrame().select(literal_expr).item()
    print("Extracting an float literal:", result)
    

    Here,

    <a id="h-passing-extracted-value-to-a-function"></a>

    Passing Extracted Value to a Function

    Once you extract a value from a Polars literal (pl.lit()), you can pass it to a function like any regular Python variable.

    
    import polars as pl
    
    # Define a function
    def square_number(num):
        return num ** 2
    
    # Extract integer literal
    literal_expr = pl.lit(6)
    extracted_value = pl.DataFrame().select(literal_expr).item()
    
    # Pass extracted value to the function
    result = square_number(extracted_value)
    print(result) 
    
    # Output:
    # 36
    

    Here,

    <a id="h-using-extracted-value-in-a-conditional-statement"></a>

    Using Extracted Value in a Conditional Statement

    Once you extract a value from a Polars literal using select().item(), you can use it like any native Python variable, including inside if statements and other control flow logic.

    
    import polars as pl
    
    # Define a literal expression
    literal_expr = pl.lit(20)
    
    # Extract the value
    value = pl.DataFrame().select(literal_expr).item()
    
    # Use in conditional
    if value > 10:
        print("The value is greater than 10")
    else:
        print("The value is 10 or less")
        
    # Output:
    # The value is greater than 10
    

    <a id="h-extracting-a-list-literal"></a>

    Extracting a List Literal

    When you use pl.lit() with a list in Polars, it creates a list literal expression, not an actual Python list. To work with the list as a native Python object, you need to extract it using select().item().

    
    import polars as pl
    
    # Define a list literal expression
    list_expr = pl.lit([1, 2, 3, 4, 5])
    
    # Extract the list value
    result = pl.DataFrame().select(list_expr).item()
    
    print(result) 
    print(type(result))  
    
    # Output:
    # shape: (5,)
    # Series: '' [i64]
    # [
    #	1
    #	2
    #	3
    #	4
    #	5
    # ]
    #
    

    <a id="h-using-extracted-value-in-arithmetic-operations"></a>

    Using Extracted Value in Arithmetic Operations

    When you extract a numerical Polars literal (pl.lit()) as a native Python value, you can use it in arithmetic operations just like any regular number.

    
    import polars as pl
    
    # Define an integer literal
    literal_expr = pl.lit(50)
    
    # Extract the value
    extracted_value = pl.DataFrame().select(literal_expr).item()
    
    # Perform arithmetic operations
    sum_result = extracted_value + 5
    product_result = extracted_value * 3
    power_result = extracted_value ** 2
    
    print(sum_result)     
    print(product_result) 
    print(power_result)   
    
    # Output:
    # 55
    # 150
    # 2500
    

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

    Conclusion

    In conclusion, while pl.lit() allows you to define constant values as Polars expressions, extracting the underlying Python value requires evaluating the expression within a DataFrame context. This ensures you convert the Polars expression back into a native Python type.

    Happy Learning!!

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

    Related Articles

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

    References