ts_shape.utils.coerce
Numeric coercion that reports what it could not convert.
pd.to_numeric(..., errors="coerce") turns every value it cannot parse
into NaN without a trace, so a column of "12,5" strings or a stray
"n/a" silently drops out of every downstream calculation.
:func:to_numeric_checked performs the same coercion and emits a
:class:~ts_shape.errors.DataQualityWarning naming how many non-null values
were not numeric.
:func:bool_or_false reads a run/state column as plain bool with
missing values as False, without relying on pandas' deprecated silent
downcasting of object columns (.fillna(False).astype(bool)).
:func:run_mask is the one convention every run/state detector uses to read
a run signal: a bool, a mode number or a text state, mapped with
run_states (the values that mean running) or value_range (inclusive
numeric bounds). Without a mapping, a signal that is not a plain 0/1 or
bool emits a :class:~ts_shape.errors.DataQualityWarning.
| Name | Description |
|---|---|
to_numeric_checked | Coerce values to numbers, warning when non-null values become NaN. |
bool_or_false | values as a NumPy bool Series; missing values are False. |
run_mask | Read a run signal as a nullable boolean Series (True = running). |
run_bool | :func:run_mask as a NumPy bool Series; unknown values are False. |
to_numeric_checked
to_numeric_checked(values: pd.Series, *, what: str) -> pd.Series
Coerce values to numbers, warning when non-null values become NaN.
| Name | Type | Description | Default |
|---|---|---|---|
values | Series | Values to coerce. | required |
what | str | Short description of the values for the warning message, e.g. "counter column 'value_integer'". | required |
| Type | Description |
|---|---|
Series | A numeric Series aligned with values. Values that are not numeric |
Series | are NaN. |
| Type | Description |
|---|---|
DataQualityWarning | When at least one non-null value is not numeric. |
bool_or_false
bool_or_false(values: pd.Series) -> pd.Series
values as a NumPy bool Series; missing values are False.
Known values are cast with bool (as .astype(bool) does), so the
result equals values.fillna(False).astype(bool) for every dtype, but
an object column is never filled first, which pandas 2.2 reports as
a deprecated downcast.
| Name | Type | Description | Default |
|---|---|---|---|
values | Series | State values (bool, nullable boolean, numbers or object). | required |
| Type | Description |
|---|---|
Series | A bool Series with the index and name of values. |
run_mask
run_mask(
values: pd.Series,
*,
run_states: Collection[Any] | None = None,
value_range: tuple[float | None, float | None] | None = None,
what: str | None = None,
warn: bool = True
) -> pd.Series
Read a run signal as a nullable boolean Series (True = running).
| Name | Type | Description | Default |
|---|---|---|---|
values | Series | The run signal's values (bool, numbers or text). | required |
run_states | Collection[Any] | None | Values that mean running (e.g. {2} for a mode signal where 2 is automatic, or {"RUN", "AUTO"}); every other known value is not running. Numbers match across int and float (2 matches 2.0). | None |
value_range | tuple[float | None, float | None] | None | (lower, upper) inclusive bounds; a value inside counts as running (either bound may be None). Used when run_states is None. | None |
what | str | None | Description of the signal for the warning (e.g. "OEEEvents: run signal 'mode'"). | None |
warn | bool | Emit the warning for an unmapped signal (default True). | True |
| Type | Description |
|---|---|
Series | A boolean Series aligned with values; a missing value is |
Series | pd.NA (unknown). |
| Type | Description |
|---|---|
DataQualityWarning | When neither run_states nor value_range is given and the values are text or numbers other than 0/1: the bool cast then counts every non-zero number and every non-empty text ("Stopped" too) as running. |
run_bool
run_bool(
values: pd.Series,
*,
run_states: Collection[Any] | None = None,
value_range: tuple[float | None, float | None] | None = None,
what: str | None = None,
warn: bool = True
) -> pd.Series
:func:run_mask as a NumPy bool Series; unknown values are False.