Skip to main content
Version: 0.0.98

ts_shape.loader.context.context_enricher

Classes
NameDescription
ContextEnricherEnrich timeseries DataFrames with contextual metadata.

ContextEnricher​

classsource
ContextEnricher(dataframe: pd.DataFrame, *, uuid_column: str = 'uuid') -> None

Enrich timeseries DataFrames with contextual metadata.

Merges metadata (descriptions, units, tolerances, value mappings) onto timeseries data using UUID-based lookups. This enables downstream analytics to access context without separate lookups.

Each enrich_* call works on the result of the previous one, so the calls chain; :meth:get_enriched_dataframe returns the cumulative result. Keys are compared as text (a numeric uuid column matches string uuids). A lookup table with duplicate keys, or a column that already exists in the data, raises ValueError instead of silently picking a row or adding _x / _y columns.

Example usage::

enricher = ContextEnricher(timeseries_df)

# Add descriptions and units from metadata
enricher.enrich_with_metadata(
metadata_df,
metadata_uuid_column="uuid",
columns=["description", "unit", "area"],
)

# Add tolerance limits (on top of the metadata columns)
enricher.enrich_with_tolerances(
tolerance_df,
tolerance_uuid_column="uuid",
lower_limit_column="lower_limit",
upper_limit_column="upper_limit",
)
enriched = enricher.get_enriched_dataframe()
Functions
NameDescription
enrich_with_metadataJoin metadata columns onto timeseries by UUID.
enrich_with_tolerancesAttach tolerance limits from a tolerance DataFrame.
enrich_with_mappingApply value mappings from a mapping DataFrame.
get_enriched_dataframeReturn the cumulative result of every enrich_* call so far.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameTimeseries DataFrame with a UUID column.required
uuid_columnstrColumn name for signal UUID.'uuid'

enrich_with_metadata​

methodsource
enrich_with_metadata(
metadata: pd.DataFrame,
*,
metadata_uuid_column: str = "uuid",
columns: list[str] | None = None
) -> pd.DataFrame

Join metadata columns onto timeseries by UUID.

Parameters
NameTypeDescriptionDefault
metadataDataFrameMetadata DataFrame containing signal info.required
metadata_uuid_columnstrUUID column in metadata (formerly metadata_uuid_col).'uuid'
columnslist[str] | NoneSpecific columns to merge. If None, merges all non-UUID columns.None
Returns
TypeDescription
DataFrameThe enriched DataFrame (also kept for the next enrich_* call).
Raises
TypeDescription
ColumnNotFoundErrorWhen a named column is missing.
ValueErrorFor duplicate uuids in metadata or a column that already exists in the data.

enrich_with_tolerances​

methodsource
enrich_with_tolerances(
tolerances: pd.DataFrame,
*,
tolerance_uuid_column: str = "uuid",
lower_limit_column: str | None = None,
upper_limit_column: str | None = None
) -> pd.DataFrame

Attach tolerance limits from a tolerance DataFrame.

The limits are added as lower_limit and upper_limit. A limit column of the table with another name is added too, under its own name, right before its canonical column: with the former default layout (low_limit / high_limit) the frame reads low_limit, lower_limit, high_limit, upper_limit. low_limit and high_limit are deprecated twins, removed in the next major release.

Parameters
NameTypeDescriptionDefault
tolerancesDataFrameDataFrame with uuid, lower_limit and upper_limit columns (or the former low_limit and high_limit).required
tolerance_uuid_columnstrUUID column in tolerances (formerly tolerance_uuid_col).'uuid'
lower_limit_columnstr | NoneColumn with the lower tolerance limit (formerly low_col). Default: "lower_limit", or "low_limit" when the table has only that one.None
upper_limit_columnstr | NoneColumn with the upper tolerance limit (formerly high_col). Default: "upper_limit", or "high_limit" when the table has only that one.None
Returns
TypeDescription
DataFrameThe enriched DataFrame (also kept for the next enrich_* call).
Raises
TypeDescription
ColumnNotFoundErrorWhen a named column is missing.
ValueErrorFor duplicate uuids in tolerances or a limit column that already exists in the data.

enrich_with_mapping​

methodsource
enrich_with_mapping(
mapping: pd.DataFrame,
*,
mapping_uuid_column: str = "uuid",
value_column_raw: str = "raw_value",
value_column_mapped: str = "mapped_value",
value_column: str = "value_string"
) -> pd.DataFrame

Apply value mappings from a mapping DataFrame.

Maps raw values (e.g. numeric codes) to descriptive strings using a UUID-specific mapping table. Raw values are compared as text, so the code 1 (integer) matches the raw value "1".

Parameters
NameTypeDescriptionDefault
mappingDataFrameDataFrame with uuid, raw_value, mapped_value columns.required
mapping_uuid_columnstrUUID column in mapping (formerly mapping_uuid_col).'uuid'
value_column_rawstrColumn of mapping with the raw values to match (formerly raw_value_col).'raw_value'
value_column_mappedstrColumn of mapping with the mapped/descriptive values (formerly mapped_value_col).'mapped_value'
value_columnstrColumn in timeseries to match against raw values.'value_string'
Returns
TypeDescription
DataFrameThe DataFrame with a mapped_value column appended (also kept
DataFramefor the next enrich_* call).
Raises
TypeDescription
ColumnNotFoundErrorWhen a named column is missing.
ValueErrorFor duplicate (uuid, raw value) pairs, or when the data already has a mapped_value column.

get_enriched_dataframe​

methodsource
get_enriched_dataframe() -> pd.DataFrame

Return the cumulative result of every enrich_* call so far.