Some functions you write in your code may be too specific to exist as built-in Polars methods or expressions. Fortunately, Polars provides a way to apply your custom function to a dataframe within a chained operation. Below is a dataframe of Apple stock data.
shape: (10, 2)
Date
Open
date
f64
2024-01-02
187.149994
2024-01-03
184.220001
2024-01-04
182.149994
2024-01-05
181.990005
2024-01-08
182.089996
2024-01-09
183.919998
2024-01-10
184.350006
2024-01-11
186.539993
2024-01-12
186.059998
2024-01-16
182.160004
Create custom function
Suppose you have the following custom function to calculate the change in value between the previous day’s Open and the current day’s Open.