Temperature conversionΒΆ

Unlike meters and seconds, fahrenheits, celsius and kelvin are not multiplicative units. Temperature is expressed in a system with a reference point, and relations between temperature units include not only an scaling factor but also an offset. Pint supports these type of units and conversions between them. The default definition file includes fahrenheits, celsius, kelvin and rankine abbreviated as degF, degC, degK, and degR.

For example, to convert from celsius to fahrenheit:

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> home = 25.4 * ureg.degC
>>> print(home.to('degF'))
77.72000039999993 degF

or to other kelvin or rankine:

>>> print(home.to('degK'))
298.54999999999995 degK
>>> print(home.to('degR'))
537.39 degR

Additionally, for every temperature unit in the registry, there is also a delta counterpart to specify differences. For example, the change in celsius is equal to the change in kelvin, but not in fahrenheit (as the scaling factor is different).

>>> increase = 12.3 * ureg.delta_degC
>>> print(increase.to(ureg.delta_degK))
12.3 delta_degK
>>> print(increase.to(ureg.delta_degF))
6.833333333333334 delta_degF

Differences in temperature are multiplicative:

>>> speed = 60. * ureg.delta_degC / ureg.min
>>> print(speed.to('delta_degC/second'))
1.0 delta_degC / second

The parser knows about delta units and use them when a temperature unit is found in a multiplicative context. For example, here:

>>> print(ureg.parse_units('degC/meter'))
delta_degC / meter

but not here:

>>> print(ureg.parse_units('degC'))
degC

You can override this behaviour:

>>> print(ureg.parse_units('degC/meter', to_delta=False))
degC / meter

To define a new temperature, you need to specify the offset. For example, this is the definition of the celsius and fahrenheit:

degC = degK; offset: 273.15 = celsius
degF = 9 / 5 * degK; offset: 255.372222 = fahrenheit

You do not need to define delta units, as they are defined automatically.

Logo

About Pint

Units in Python. You are currently looking at the documentation of version 0.4.2.

Other Formats

You can download the documentation in other formats as well:

Useful Links

Related Topics

Fork me on GitHub