Skip to main content
Version: 0.0.98

ts_shape.transform.calculator.unit_conversion

Engineering unit conversion for ts-shape, backed by the pint library.

:class:UnitConverter is a thin wrapper around pint <https://pint.readthedocs.io>_, the de-facto-standard, actively maintained open-source Python units library. pint owns all conversion data, dimensional analysis, affine temperature handling and SI prefixes -- ts-shape stores no conversion factors of its own.

pint is an optional dependency; install it with::

pip install ts-shape[units]
Classes
NameDescription
UnitConverterConvert engineering units in scalars and DataFrame columns via pint.
Functions
NameDescription
register_unit_aliasAccept alias (a plant's unit spelling) as unit (a pint unit).

register_unit_alias​

functionsource
register_unit_alias(alias: str, unit: str) -> None

Accept alias (a plant's unit spelling) as unit (a pint unit).

Parameters
NameTypeDescriptionDefault
aliasstrThe spelling found in the data or metadata (e.g. "min1").required
unitstrThe pint unit it means (e.g. "1/minute").required

UnitConverter​

classsource

Bases: Base

Convert engineering units in scalars and DataFrame columns via pint.

Unit names are whatever pint understands (e.g. "bar", "psi", "m^3/hour", "kWh", "degC"). The temperature shorthands C, F, K and R are accepted as conveniences.

Example usage::

UnitConverter.convert_value(100, "C", "F") # 212.0
UnitConverter.conversion_factor("bar", "psi") # (14.5037..., 0.0)
UnitConverter.convert_column(df, "bar", "psi", value_column="value_double")
Example
>>> import pandas as pd
>>> from ts_shape import UnitConverter
>>> round(UnitConverter.convert_value(100, "C", "F"), 6)
212.0
>>> factor, offset = UnitConverter.conversion_factor("bar", "psi")
>>> round(factor, 4), offset
(14.5038, 0.0)
>>> df = pd.DataFrame({
... "systime": pd.date_range("2025-01-01", periods=3, freq="1h"),
... "uuid": "pressure",
... "value_double": [2.0, 1.5, 0.0],
... })
>>> UnitConverter.convert_column(df, "bar", "kPa")[["systime", "value_double"]]
systime value_double
0 2025-01-01 00:00:00 200.0
1 2025-01-01 01:00:00 150.0
2 2025-01-01 02:00:00 0.0
Functions
NameDescription
convert_valueConvert a single scalar value from from_unit to to_unit.
conversion_factorReturn the (scale, offset) mapping from_unit -> to_unit.
convert_columnConvert a numeric DataFrame column to different units.

convert_value​

classmethodsource
convert_value(value: float, from_unit: str, to_unit: str) -> float

Convert a single scalar value from from_unit to to_unit.

Parameters
NameTypeDescriptionDefault
valuefloatThe numeric value to convert.required
from_unitstrSource unit (any pint unit name).required
to_unitstrTarget unit (any pint unit name).required
Returns
TypeDescription
floatThe converted value as a float.
Raises
TypeDescription
ImportErrorIf the pint library is not installed.
ValueErrorIf a unit is unknown or the units are incompatible.

conversion_factor​

classmethodsource
conversion_factor(from_unit: str, to_unit: str) -> tuple[float, float]

Return the (scale, offset) mapping from_unit -> to_unit.

The conversion is target = value * scale + offset. offset is 0.0 for pure-ratio units and non-zero for affine units such as temperature. The pair is derived automatically from pint.

Parameters
NameTypeDescriptionDefault
from_unitstrSource unit.required
to_unitstrTarget unit.required
Returns
TypeDescription
tuple[float, float](scale, offset) as floats.
Raises
TypeDescription
ImportErrorIf the pint library is not installed.
ValueErrorIf a unit is unknown or the units are incompatible.

convert_column​

classmethodsource
convert_column(
dataframe: pd.DataFrame,
from_unit: str,
to_unit: str,
*,
value_column: str = "value_double",
target_column: str | None = None
) -> pd.DataFrame

Convert a numeric DataFrame column to different units.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameThe DataFrame to operate on.required
from_unitstrSource unit of the column values.required
to_unitstrTarget unit.required
value_columnstrColumn holding the values to convert.'value_double'
target_columnstr | NoneColumn to write the converted values to. Defaults to value_column (in-place on a copy).None
Returns
TypeDescription
DataFrameA copy of the DataFrame with the converted column.
Raises
TypeDescription
ImportErrorIf the pint library is not installed.
ColumnNotFoundErrorIf value_column is missing.
ValueErrorIf a unit is unknown or the units are incompatible.
Warns
TypeDescription
DataQualityWarningWhen values are not numbers; they become NaN.