Events (dv.events)

Event markers for instants and periods, and their conversion to flags and plot overlays.

Imported as diive.events, reached as dv.events after import diive as dv.

EVENTS: TIME-STAMPED EVENT MARKERS

Represent things that happened at a point in time or over a period — anything the user wants to mark (management interventions, instrument swaps, field campaigns, weather events, …) — as 0/1 data columns and as overlays on time-series plots.

Part of the diive library: https://github.com/holukas/diive

class diive.events.Event(name, start, end=None, category='', description='', color=None)[source]

Bases: object

A single time-stamped event.

An event is either an instant (end is None — a single point in time, e.g. something that happened on one day) or a period (end set — e.g. an interval, or a start date plus a duration). start/end are coerced to pandas.Timestamp; a period with end < start is rejected.

Parameters:
  • name (str) – Short label shown on plots and used to build the flag column name.

  • start (Timestamp) – When the event began (or the instant it occurred).

  • end (Timestamp | None) – When the event ended; None for an instant event.

  • category (str) – Optional grouping (e.g. "category1") that drives the default colour via CATEGORY_COLORS.

  • description (str) – Free-text note.

  • color (str | None) – Explicit colour; when None it is resolved from the category.

name: str
start: Timestamp
end: Timestamp | None = None
category: str = ''
description: str = ''
color: str | None = None
property is_range: bool

True for a period event (has an end), False for an instant.

property duration: Timedelta

Length of the event (zero for an instant).

property flag_name: str

The 0/1 flag column name for this event (see make_event_flag_name()).

resolved_color(fallback_index=0, colors=None)[source]

The colour to draw this event in: explicit, else by category, else a cycled fallback (indexed by fallback_index so a list of events with no category still get distinct colours).

colors is an optional {category: hex} override map consulted before the built-in CATEGORY_COLORS (category match is case-insensitive), so a caller can supply its own category palette without mutating the event.

Return type:

str

Parameters:
to_dict()[source]

Serialise to a plain dict (ISO timestamps) for persistence.

Return type:

dict

classmethod from_dict(data)[source]

Rebuild an Event from to_dict() output.

Return type:

Event

Parameters:

data (dict)

__init__(name, start, end=None, category='', description='', color=None)
Parameters:
Return type:

None

diive.events.event_to_flag(event, index, name=None)[source]

Build the yes/no (0/1) flag series for an event, aligned to index.

The result is 1 where the event was active and 0 everywhere else — a normal data column that can be saved, plotted, and processed like any other variable.

  • Period event (event.end set): 1 for every record in [start, end] (inclusive).

  • Instant event (event.end is None): 1 at the single record nearest to event.start (so a point event always marks exactly one record, even when its timestamp falls between two records).

Parameters:
  • event (Event) – The event to encode.

  • index (DatetimeIndex) – Target timestamp index (the dataset’s index).

  • name (str | None) – Optional series name; defaults to event.flag_name.

Return type:

Series

Returns:

Integer Series of 0/1 indexed by index.

diive.events.make_event_flag_name(name)[source]

Column name for an event’s 0/1 flag: EVENT_<sanitized name>.

Whitespace collapses to single underscores so the result is a clean column label; other characters are kept (diive column names are otherwise free-form).

Return type:

str

Parameters:

name (str)

diive.events.overlay_events(ax, events, *, axis='x', show_labels=True, label_fontsize=8, line_alpha=0.9, span_alpha=0.16, linewidth=1.3, colors=None)[source]

Draw events onto an existing matplotlib axes.

Instant events become a dashed line; period events become a translucent shaded span bordered by thin start/end lines. Positions are converted with matplotlib.dates.date2num(), which matches the float units of any diive datetime axis — both the value-vs-time line panels (axis='x') and the date/time heatmap, where the date is the y-axis (axis='y').

This only adds artists; it never clears or rescales the axes, so it composes on top of an already-rendered plot. The overlays are drawn above any image or mesh already on the axes, so they stay visible on an opaque heatmap.

Parameters:
  • ax – Target axes (already drawn).

  • events – Iterable of Event.

  • axis (str) – 'x' to draw against time on the x-axis (line/cumulative/daily panels), 'y' to draw against date on the y-axis (heatmap).

  • show_labels (bool) – Annotate each event with its name.

  • label_fontsize (int) – Font size for the labels.

  • line_alpha (float) – Opacity of instant/border lines.

  • span_alpha (float) – Opacity of the period shading.

  • linewidth (float) – Width of instant/border lines.

  • colors (dict[str, str] | None) – Optional {category: hex} override map (see Event.resolved_color()).

Return type:

None