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 –
pl.lit()
creates a Polars expression, not a direct Python value.- To get the native Python value, you must evaluate the expression using a DataFrame.
- Use
select(…).item()
to extract a scalar value from the expression. - This approach works for various data types: integers, floats, strings, booleans, and lists.
- Extracted values can be used in arithmetic, string manipulation, or logic operations.
- The extracted result is a standard Python object, fully usable outside the Polars context.
- This method is commonly used when testing, debugging, or integrating with external functions.
- Attempting to directly operate on
pl.lit()
without extraction won’t yield Python-native behavior.
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.
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,
pl.lit(42)
creates a Polars literal expression representing the integer42
.select(literal_expr)
evaluates the expression inside a temporary DataFrame.item()
extracts the computed result as a native Python integer.
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,
pl.lit(3.14159)
creates a Polars expression representing the float3.14159
.select(literal_expr)
evaluates the expression inside a temporary DataFrame.item()
extracts the computed result as a native Python float.
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,
pl.lit(6)
creates a Polars expression.select().item()
extracts the integer value (6
).- The extracted value is passed to
square_number()
.
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
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
# ]
#<class 'polars.series.series.Series'>
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
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!!
Related Articles
- How to Select Last Column of Polars DataFrame
- Polars Filter by Column Value
- Convert Polars String to Integer
- Polars Rename Columns to Lowercase
- How to Change Position of a Column in Polars
- Reorder Columns in a Specific Order Using Polars
- How to Convert a Polars DataFrame to Python List?
- Append or Concatenate Two DataFrames in Polars
- How to Add a Column with Numerical Value in Polars
- Get First N Characters from a String Column in Polars