Questions tagged [pint]

Pint is a Python units library to perform calculations with physical quantities. Use this tag in conjunction with the [python] tag for specific programming questions using Pint.

Pint is Python package to define, operate and manipulate physical quantities: the product of a numerical value and a unit of measurement. It allows arithmetic operations between them and conversions from and to different units.

It is distributed with a comprehensive list of physical units, prefixes and constants. Due to its modular design, you can extend (or even rewrite!) the complete list without changing the source code. It supports a lot of numpy mathematical operations without monkey patching or wrapping numpy.

It has a complete test coverage. It runs in Python 2.6+ and 3.2+ with no other dependency. It licensed under BSD.

Source, more information and latest release:
https://pint.readthedocs.io/

77 questions
9
votes
4 answers

How to define and use percentage in Pint

I'm currently using Pint to handle units and unit conversions. This seems to work well for the units that are already defined in Pint, for example >>> import pint >>> ureg = pint.UnitRegistry() >>> Q = ureg.Quantity >>> a = Q(5, 'm/s') >>>…
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
4
votes
3 answers

Format Pint unit as short-form symbol

Say I have an arbitrary Pint quantity q. Is there a way to display its units in symbol short form, instead of as a full-length word? In other words, how would I code unit_symbol() such that it returns "m", not "meter"; "kg" not "kilogram"; etc.? Is…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
4
votes
1 answer

if statement: whether an array has units/dimensions (pint)

My variable C0 is defined in pint.UnitRegistry units mol/L. I need to use it in a function, but in order for Python to not call me on inconsistent units for the general function, I have to define a new variable in the function with units, as…
4
votes
1 answer

In pint, how do I redefine the micro- symbol to be µ-, not u-?

In pint, how do I redefine the micro- prefix to be spelt µ- rather than u-? Both are aliases when defining units, but when obtaining the short symbol such as with the {:~} format specification, pint reverts to u- even when I try to redefine micro-…
gerrit
  • 24,025
  • 17
  • 97
  • 170
3
votes
0 answers

Simultaneously handling uncertainties and units in python

I building a python library to analyze data (e.g. spectral data) with units and uncertainties. For example, we have data on wavelength (nm), counts, and power (W). I want to be able to easily convert between units e.g. from wavelength in nm to um. I…
Selewirre
  • 41
  • 3
3
votes
1 answer

Standard format for quantity in derived dimension in `pint`

TLDR: I'd like to have pint quantities, that are in a certain (derived) dimension, to be converted into a pre-set unit by default. Details: I deal with 5 dimensions, as specified below. Note that [power] (usually in MW) and [price] (usually in…
ElRudi
  • 2,122
  • 2
  • 18
  • 33
3
votes
2 answers

Test for unit equivalence in pint

How do I test for unit equivalence in Pint? For example, nM is equivalent to nmol/L and L is equivalent to dm^3, but they are not equal according to Pint. I don't want compatibility, which Pint provides via the is_compatible_with method. For…
drhagen
  • 8,331
  • 8
  • 53
  • 82
3
votes
1 answer

Parse just unit with pint

The pint docs are clear about how to parse a quantity like "1 meter". How do I parse just a unit, like "meter"? All of these return a quantity with a value of 1: import pint ureg = pint.UnitRegistry() ureg('meter') # Quantity 1 meter ureg['meter']…
drhagen
  • 8,331
  • 8
  • 53
  • 82
3
votes
0 answers

Prevent simplification of units in pint

I sometimes use the pint library to display civil engineering calculations. For these calculations, there are times when I want a quantity displayed a certain way in order to make it clear what the quantity represents. But when using pint, the units…
Rick
  • 43,029
  • 15
  • 76
  • 119
3
votes
1 answer

Dimensional analysis in SymPy

I'm trying to use SymPy to do the following tasks: Simplify some algebraic equations of physical quantities and physical constants. Perform dimensional analysis to ensure my equations are correct Evaluate the equations by plugging in values for the…
MountainDrew
  • 443
  • 4
  • 20
3
votes
1 answer

Python Pint doesn't merge units of the same quantity when multiplying

Using Python Pint, I'm getting a strange result when multiplying units of the same quantity. You would expect them to merge, but they don't. For example: from pint import UnitRegistry units = UnitRegistry() Then: (3 * units.m) * ( 5 * units.m) …
3
votes
2 answers

pint significant figures, precision

I'm looking for a way to tell pint how many significant figures to print. For instance, when I type this: import pint ureg = pint.UnitRegistry() print(3*ureg.m /9) 0.3333333333333333 meter You can see that the printed representation has many…
Ethan Keller
  • 963
  • 3
  • 10
  • 26
3
votes
3 answers

Python - pint - Can I set default type to Decimal?

Im using pint module in a project. Objects in my project handle numerical data as Decimals. When I set simple pint units to a Decimal, it works: >>> import pint >>> from decimal import Decimal as D >>> ureg = pint.UnitRegistry() >>> D(10) *…
echefede
  • 497
  • 4
  • 13
3
votes
1 answer

Python pint module with multiprocessing

The python pint module implements physical quantities. I would like to use it together with multiprocessing. However, I don't know how to handle creating a UnitRegistry in the new process. If I do the intuitive: from multiprocessing import…
Peter
  • 543
  • 1
  • 5
  • 13
2
votes
2 answers

numpy stack not working for astropy quantities in numpy 1.23

The following works in numpy 1.23.5 but not in 1.24.3: from astropy import units as u import numpy as np a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) np.stack([a,b]*u.m) With numpy 1.24.3, I get: Traceback (most recent call last): …
Dunbur
  • 23
  • 5
1
2 3 4 5 6