Skip to main content
Version: 0.0.98

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.

Functions
NameDescription
to_numeric_checkedCoerce values to numbers, warning when non-null values become NaN.
bool_or_falsevalues as a NumPy bool Series; missing values are False.
run_maskRead 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​

functionsource
to_numeric_checked(values: pd.Series, *, what: str) -> pd.Series

Coerce values to numbers, warning when non-null values become NaN.

Parameters
NameTypeDescriptionDefault
valuesSeriesValues to coerce.required
whatstrShort description of the values for the warning message, e.g. "counter column 'value_integer'".required
Returns
TypeDescription
SeriesA numeric Series aligned with values. Values that are not numeric
Seriesare NaN.
Warns
TypeDescription
DataQualityWarningWhen at least one non-null value is not numeric.

bool_or_false​

functionsource
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.

Parameters
NameTypeDescriptionDefault
valuesSeriesState values (bool, nullable boolean, numbers or object).required
Returns
TypeDescription
SeriesA bool Series with the index and name of values.

run_mask​

functionsource
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).

Parameters
NameTypeDescriptionDefault
valuesSeriesThe run signal's values (bool, numbers or text).required
run_statesCollection[Any] | NoneValues 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_rangetuple[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
whatstr | NoneDescription of the signal for the warning (e.g. "OEEEvents: run signal 'mode'").None
warnboolEmit the warning for an unmapped signal (default True).True
Returns
TypeDescription
SeriesA boolean Series aligned with values; a missing value is
Seriespd.NA (unknown).
Warns
TypeDescription
DataQualityWarningWhen 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​

functionsource
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.