Chalk home page
Docs
API
CLI
  1. Resolvers
  2. Time Filtering

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 before and after. 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 point-in-time correctness.

Chalk supplies two functions, before and after, 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 DataFrame:

After

To compute the number of transfers a user made in the last seven days, you can use the after(...) function:

from chalk.features import after, ...

@online
def fn(transfers: User.transfers[after(days_ago=7)]) -> ...:
    return transfers.count()

Before

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 before, ...

@online
def fn(transfers: User.transfers[before(days_ago=7)]) -> ...:
    return transfers.count()

Combining before and after

You can also combine before and after. In this example, we return the transfers made over a week ago, but less than two weeks ago. These filters can also be combined with other filters and projections as described here.

from chalk.features import before, after, ...

@online
def fn(transfers: User.transfers[after(days_ago=14), before(days_ago=7)]) -> ...:
    return transfers.count()