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) orfloat64; - numbers stored as text in
value_double/value_integerare parsed, with a :class:~ts_shape.errors.DataQualityWarningcounting the values that are not numbers; - boolean value columns become
bool(no missing values) orobjectholdingTrue/False/NaN, as pandas builds them from NumPy data; - string columns (
value_string, the uuid column) becomeobject; - a pyarrow timestamp time column becomes
datetime64.
| Name | Description |
|---|---|
id_text | Identifier values (part numbers, order ids) as text. |
unique_text | set(series.dropna().astype(str).unique()), from the unique values. |
text_mask | Rows whose value, as text, equals text (missing values never match). |
text_keys | series.astype(str).to_numpy() for a column without missing values. |
normalize_signal_frame | Return frame with NumPy-backed dtypes (see the module docstring). |
rows_normalize_alike | Whether :func:normalize_signal_frame treats every row on its own. |
id_text
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
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
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
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
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.
| Name | Type | Description | Default |
|---|---|---|---|
frame | DataFrame | Long-format signal data (a copy owned by the caller). | required |
time_column | str | Timestamp column. | 'systime' |
uuid_column | str | Signal identifier column. | 'uuid' |
| Type | Description |
|---|---|
DataFrame | The same frame object, with converted columns. |
rows_normalize_alike
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.