API Reference
This page documents the public API of safefeat.
build_features
safefeat.core.build_features
build_features(
spine,
tables,
spec,
*,
entity_col="entity_id",
cutoff_col="cutoff_time",
event_time_cols=None,
allowed_lag="0s",
return_report=False,
)
Build leakage-safe features from event tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spine
|
DataFrame
|
DataFrame containing entity identifiers and cutoff times. |
required |
tables
|
dict[str, DataFrame]
|
Mapping of table name to event DataFrame. |
required |
spec
|
FeatureSpec or list[WindowAgg]
|
Feature specification describing windows and aggregations. |
required |
entity_col
|
str
|
Name of entity identifier column. |
"entity_id"
|
cutoff_col
|
str
|
Name of cutoff timestamp column. |
"cutoff_time"
|
event_time_cols
|
dict[str, str]
|
Mapping of table name to event timestamp column. |
None
|
allowed_lag
|
str
|
Allowed tolerance for future timestamps (pandas timedelta string). |
"0s"
|
return_report
|
bool
|
If True, return a tuple |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame or (DataFrame, AuditReport)
|
Feature matrix aligned to the spine. If |
Source code in src/safefeat/core.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
Feature Specification
WindowAgg
safefeat.spec.WindowAgg
dataclass
Specification for aggregating events within a time window.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
str
|
Name of the events table to read (key in the |
windows |
List[Optional[str]]
|
List of window lengths expressed as duration strings (e.g.
For each window a set of features will be produced. How M and Y windows are calculated
The window is a sliding lookback from the cutoff — it does not snap to calendar month or year boundaries. Given a cutoff date, the window start is computed as::
Events are included if Examples:
.. note::
Use |
metrics |
Dict[str, List[str]]
|
Mapping from a column name to a list of aggregations to compute. Use
|
Examples:
import pandas as pd
from safefeat import build_features, WindowAgg
spine = pd.DataFrame({
"entity_id": ["u1"],
"cutoff_time": ["2024-01-10"],
})
events = pd.DataFrame({
"entity_id": ["u1", "u1"],
"event_time": ["2024-01-05", "2024-01-08"],
"amount": [10, 20],
})
spec = [
WindowAgg(
table="events",
windows=["7D", "3M", "1Y"],
metrics={"amount": ["sum"], "*": ["count"]},
)
]
X = build_features(
spine=spine,
tables={"events": events},
spec=spec,
event_time_cols={"events": "event_time"},
)
# column names produced:
[events__n_events__7d
events__amount__sum__7d
events__n_events__3m
events__amount__sum__3m
events__n_events__1y
events__amount__sum__1y]
Source code in src/safefeat/spec.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
RecencyBlock
safefeat.spec.RecencyBlock
dataclass
Specification for computing time since the most recent event.
This block computes the number of days between the cutoff and the most
recent matching event for each entity-cutoff pair. Optionally the block
can be restricted to events that match filter_col == filter_value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Name of the events table to use. |
required |
filter_col
|
Optional[str]
|
Optional name of a column to filter on (for example |
None
|
filter_value
|
Optional[str]
|
Optional value that |
None
|
Examples:
import pandas as pd
from safefeat import build_features, RecencyBlock
spine = pd.DataFrame({
"entity_id": ["u1"],
"cutoff_time": ["2024-01-10"],
})
events = pd.DataFrame({
"entity_id": ["u1"],
"event_time": ["2024-01-08"],
})
spec = [RecencyBlock(table="events")]
X = build_features(
spine=spine,
tables={"events": events},
spec=spec,
event_time_cols={"events": "event_time"},
)
X["events__recency"].iloc[0]
# 2
Source code in src/safefeat/spec.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
Example
1. Basic window features (count, sum, mean)
import pandas as pd
from safefeat import build_features, WindowAgg
spine = pd.DataFrame({
"entity_id": ["u1"],
"cutoff_time": ["2024-01-10"],
})
events = pd.DataFrame({
"entity_id": ["u1", "u1", "u1", "u1"],
"event_time": ["2024-01-05", "2024-01-06", "2023-01-01", "2024-01-20"],
"amount": [10.0, 20.0, 999.0, 999.0],
})
X = build_features(
spine=spine,
tables={"events": events},
spec=[
WindowAgg(
table="events",
windows=["7D", "30D"],
metrics={
"*": ["count"],
"amount": ["sum", "mean"],
},
)
],
event_time_cols={"events": "event_time"},
)
print(X)
Expected output :
| entity_id | cutoff_time | events__n_events__7d | events__amount__sum__7d | events__amount__mean__7d | events__n_events__30d | events__amount__sum__30d | events__amount__mean__30d |
| --------- | ----------- | -------------------- | ----------------------- | ------------------------ | --------------------- | ------------------------ | ------------------------- |
| u1 | 2024-01-10 | 2 | 30.0 | 15.0 | 2 | 30.0 | 15.0 |