monad.targets.slice_time_window
monad.targets.slice_time_windowmonad.targets.slice_time_window(events, start, end)
Restricts the events to a desired time range.
This function takes an explicit start and end timestamp and trims the event streams passed into your target function to that interval.
Semantics:
- Half-open interval – [start, end) includes start, excludes end.
- Empty/out-of-range windows – safe, returns empty events.
- Supported types – numeric Unix timestamps (seconds) or datetimes converted to timestamps.
- Time zones – always use UTC.
- Multiple streams – each stream is sliced independently.
from datetime import datetime
from monad.targets import slice_time_window
def target_fn(_history: Events, future: Events, _entity: Attributes, _ctx: Dict) -> np.ndarray:
start = datetime(2020, 3, 15).timestamp() # inclusive
end = datetime(2020, 4, 5).timestamp() # exclusive
future_constrained = next_n_hours(events=future, start=start, end=end)
...Edge Cases:
- start ≥ end → returns empty events.
- Window outside range → empty events.
- Duplicate timestamps → all values with start ≤ t < end are included.
Best Practices:
- Convert datetimes to UTC numeric timestamps before slicing.
- Apply slicing at the target function input level; do not preprocess upstream.
- Use with care when training — slicing reduces available samples and can affect variance.
Interactions :
- Target functions – slicing acts as a pre-filter before your computations.
- Temporal splits – can be combined with history/future splits to constrain evaluation periods.\
- Date handling improvements – supports numeric date formats and correct boundary handling.
Troubleshooting:
- Empty slices → verify timestamp units (seconds vs. ms) and overlap.
- Boundary issues → remember end is exclusive.
| Parameters |
|---|
events : Events
Events to filter.
start : float
The timestamp indicating the inclusive lower bound of the time window for events to be considered after applying the filter.
end: float
The timestamp indicating the exclusive upper bound of the time window for events to be considered after applying the filter.
| Returns |
|---|
Events restricted to the chosen time range.
