Skip to main content
Version: 0.0.98

ts_shape.utils.normalize

Bring a long-format signal frame to the dtypes the detectors compute with.

Data read from parquet, databases or CSV arrives in many dtypes: pandas nullable types (Int64, Float64, boolean, string), pyarrow types (int64[pyarrow], bool[pyarrow]), or numbers stored as text. pandas arithmetic on these differs from NumPy in ways that change results without an error: a comparison with a missing value is <NA> instead of False, cumsum is not defined for bool[pyarrow], and text numbers concatenate instead of adding up.

:func:normalize_signal_frame converts once, up front, so every detector sees the same NumPy-backed dtypes:

  • numeric value columns become int64 (no missing values) or float64;
  • numbers stored as text in value_double / value_integer are parsed, with a :class:~ts_shape.errors.DataQualityWarning counting the values that are not numbers;
  • boolean value columns become bool (no missing values) or object holding True / False / NaN, as pandas builds them from NumPy data;
  • string columns (value_string, the uuid column) become object;
  • a pyarrow timestamp time column becomes datetime64.
Functions
NameDescription
id_textIdentifier values (part numbers, order ids) as text.
unique_textset(series.dropna().astype(str).unique()), from the unique values.
text_maskRows whose value, as text, equals text (missing values never match).
text_keysseries.astype(str).to_numpy() for a column without missing values.
normalize_signal_frameReturn frame with NumPy-backed dtypes (see the module docstring).
rows_normalize_alikeWhether :func:normalize_signal_frame treats every row on its own.

id_text​

functionsource
id_text(values: pd.Series) -> pd.Series

Identifier values (part numbers, order ids) as text.

A numeric identifier reads as written: 1234 and 1234.0 both become "1234" (a float with a fraction keeps it). Text is kept as it is, missing values stay missing (None), so a dictionary keyed by the text of a part number finds it whatever column the signal is in.

unique_text​

functionsource
unique_text(series: pd.Series) -> set[str]

set(series.dropna().astype(str).unique()), from the unique values.

Never converts the whole column to text: only its distinct values are turned into strings (a column with mixed non-text values falls back to the row-wise conversion).

text_mask​

functionsource
text_mask(series: pd.Series, text: str) -> np.ndarray

Rows whose value, as text, equals text (missing values never match).

The same rows as series.astype(str) == text, compared on the unique values and mapped back with isin.

text_keys​

functionsource
text_keys(series: pd.Series) -> np.ndarray

series.astype(str).to_numpy() for a column without missing values.

Built from the unique values: rows with the same value share one str.

normalize_signal_frame​

functionsource
normalize_signal_frame(
frame: pd.DataFrame,
*,
time_column: str = "systime",
uuid_column: str = "uuid"
) -> pd.DataFrame

Return frame with NumPy-backed dtypes (see the module docstring).

Only the time column, the uuid column and value_* columns are touched; other columns keep their dtype. frame is changed in place and returned.

Parameters
NameTypeDescriptionDefault
frameDataFrameLong-format signal data (a copy owned by the caller).required
time_columnstrTimestamp column.'systime'
uuid_columnstrSignal identifier column.'uuid'
Returns
TypeDescription
DataFrameThe same frame object, with converted columns.

rows_normalize_alike​

functionsource
rows_normalize_alike(
frame: pd.DataFrame,
*,
time_column: str = "systime",
uuid_column: str = "uuid"
) -> bool

Whether :func:normalize_signal_frame treats every row on its own.

True when every column it touches is either left as is or is a string column turned into object value by value. Normalizing a subset of the rows then gives exactly those rows of the normalized frame. False when a conversion depends on the whole column (nullable or pyarrow numbers and booleans pick their dtype from whether any value is missing; text numbers are parsed with a warning counting them) or a pyarrow time column is converted.