Window functions are helpful for creating features that describe recent behavior. With window functions, you can implement features such as moving averages or counts of events in a window.
Window functions are computed on a DataFrame.
A has-many feature creates a DataFrame
on a feature set, which you can filter with the special functions
These functions filter your data relative to the
current time in context. This time could be in the past
if you’re using an offline resolver.
Using window functions ensures that you maintain
Chalk supplies two functions,
which take many keyword arguments describing the time
relative to the present:
# Keep where over three years and two days old before(years_ago=3, days_ago=2) # Keep where under 5:10 minutes old after(minutes_ago=5, seconds_ago=10)
You can use these operators to filter out rows of a
To compute the number of transfers a user made in the
last seven days, you can use the
from chalk.features import after, ... @online def fn(transfers: User.transfers[after(days_ago=7)]) -> ...: return transfers.count()
Alternatively, if you wanted to compute the number of transfers a user made more than seven days ago, you would write:
from chalk.features import after, ... @online def fn(transfers: User.transfers[before(days_ago=7)]) -> ...: return transfers.count()
You can also combine
In this example, we return the transfers made over a week ago,
but less than two weeks ago.
from chalk.features import before, after, ... @online def fn(transfers: User.transfers[after(days_ago=14), before(days_ago=7)]) -> ...: return transfers.count()