Torashii Utils
Utility functions, primarily for data cleaning.
fill_features(df, features, sort_col, over_col)
Cast feature columns to numeric (float), convert NaN and inf values to null, then forward fill nulls
for each column of features, sorted on sort_col and partitioned by over_col.
Parameters
df: Polars DataFrame or LazyFrame containing columns sort_col, over_col and each of features
features: collection of strings indicating which columns of df are the feature values
sort_col: str column of df indicating how to sort
over_col: str column of df indicating how to partition
Returns
Polars LazyFrame containing the original columns with cleaned feature data
Source code in torashii/utils.py
smooth_features(df, features, sort_col, over_col, window_size)
Smooth the features columns of df by taking the rolling mean of each, sorted over sort_col and
partitioned by over_col, using window_size trailing periods for the moving average window.
Parameters
df: Polars DataFrame | LazyFrame containing columns sort_col, over_col and each of features
features: collection of strings indicating which columns of df are the feature values
sort_col: str column of df indicating how to sort
over_col: str column of df indicating how to partition
window_size: int number of time periods for the moving average
Returns
Polars LazyFrame containing the original columns, with each of features replaced with moving average
Source code in torashii/utils.py
top_n_by_group(df, n, rank_var, group_var, filter=True)
Mark the top n rows in each of group_var according to rank_var descending.
If filter is True, the returned DataFrame contains only the filtered data. If filter is False,
the returned DataFrame has all data, with an additional 'rank_mask' column indicating if that row
is in the filter.
Parameters
df: Polars DataFrame | LazyFrame n: integer indicating the top rows to take in each group rank_var: str column name to rank on group_var: tuple of str column names to group and sort on filter: boolean indicating how much data to return
Returns
Polars LazyFrame containing original columns and optional filter column