Applying UDFs
UDFs are functions defined by the user to encapsulate a block of code for reuse. Polars allows you to utilize UDFs to implement your logic in your code. The only caution is that once you use any of the methods explained in this recipe, you’ll lose parallelization, and the operations are applied row by row. That potentially leads to slow performance, depending on the size of the dataset and the complexity of your code.
In this recipe, we’ll cover how to utilize UDFs in Polars using the .
map_elements()
expression.
Getting ready
We’ll use the Contoso dataset for this recipe as well. Run the following code to read the dataset:
df = pl.read_csv('../data/contoso_sales.csv', try_parse_dates=True)
How to do it...
Here are ways for how you apply UDFs:
- Define a function that extracts the first name from a full name. Apply the function using
.map_elements()
:def get_first_name(full_name: str) -> str: ...