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
1
vote
1 answer

python pint: defined offset unit definition behaves unexpected on conversion

I'm trying to define gauge pressure (barg) as an offset unit using python/pint assuming the predefined unit (bar) refers to absolute pressure. When using the defined unit to set up a quantity in 'barg' and convert it to 'bar' there is an unexpected…
mkais
  • 11
  • 1
1
vote
2 answers

Python Pint: set short representation of units as default

Pint units are represented by default with their full name: >>> import pint >>> ureg = pint.UnitRegistry() >>> q = ureg.Quantity('3.456 m^2') >>> print(q) 3.456 meter ** 2 >>> print('The pretty representation is {:P}'.format(q)) The pretty…
mmj
  • 5,514
  • 2
  • 44
  • 51
1
vote
1 answer

Why is Pint creating a second registry?

I keep getting "Cannot operate with Quantity and Quantity of different registries." errors and I cannot figure out why. In the following code snippet, Pint creates one registry used to populate the constants/fluid dictionaries. When I create a…
Jennifer
  • 31
  • 5
1
vote
1 answer

Why am I getting a UndefinedUnitError using pint & pandas?

Following along with the documentation of pint-pandas and pint as best I could, I have an implementation which does not seem to want to work. This reproduces my UndefinedUnitError. import pint import pint_pandas ureg =…
1
vote
1 answer

Get base unit of a custom dimension

Say I define a new dimension and define new units along that dimension. In this example, I use currencies and made-up exchange rates, but could be any other custom-made dimension: import pint ureg = pint.UnitRegistry() Q_ = ureg.Quantity import…
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
1
vote
0 answers

Pint: Specify abbreviated units with "as_ratio=False"

I have a pint quantity and I want the string to appear with unit abbreviations (i.e. SI prefixes and unit letters), but without a / to denote a division. So in other words I want the string to appear as 'cm g µs⁻¹' The built-in pretty formatting…
JeffP
  • 324
  • 2
  • 13
1
vote
1 answer

Decorator Not Returning Value in Expected Units

The following code uses pint to convert a mass flow to a volumetric flow. To convert from mass to volume, the density of the gas must be calculated. The calculation returns the correct values in the desired units. import pint ureg =…
Cohan
  • 4,384
  • 2
  • 22
  • 40
1
vote
1 answer

subclassing Q_ in pint - why does the super call not take arguments?

(Python 3.7) pint / Q_ behavior I want to subclass pint's quantity class and override __init__. The standard syntax fails because apparently the arguments bubble up to the object init method (which takes no argument): # file: test.py from pint…
Leporello
  • 638
  • 4
  • 12
1
vote
1 answer

How can I perform numpy matrix multiplication with pint Quantity in python 3?

I would like to multiply a (3, 3) np.matrix by a (3,1) np.matrix that includes pint Quantity information. This code works: import numpy as np x = np.mat([[1,0,0],[0,1,0],[0,0,1]]) y = np.mat([[1],[0],[0]]) x * y >>> x * y matrix([[1], …
1
vote
1 answer

Check dimensionality of complex unit in pint

I am trying to check for dimensionality of a unit that is complex such as volume (m^3) or velocity (ft/min). How can I use the "pint.check()" method to see if a quantity is of that type of dimension? This is what I have tried: import pint ureg =…
JasonArg123
  • 188
  • 1
  • 1
  • 14
1
vote
2 answers

Why is ureg(0) equal to 1?

The following code prints '1 dimensionless': import pint ureg=pint.UnitRegistry() print(ureg(0.)) Why, Pint?
Ethan Keller
  • 963
  • 3
  • 10
  • 26
1
vote
0 answers

Check if a quantity is within some measurement in pint in python

I want to check whether a single quantity is within a measurement. Or if a measurement is within a measurement. ie, this should return True: import pint u=pint.UnitRegistry() a=(1*u.m).plus_minus(0.1, relative=True) (1.05*u.m).within(a) Is this…
Ethan Keller
  • 963
  • 3
  • 10
  • 26
1
vote
0 answers

Python pint: setting adimensional constants

Let's suppose I have a program where the user get have values of a certain dimension (e.g. length) but different units. The peculiarity is that some of those units (scalefactor and hubble) are actually adimensional constant from pint import…
Antonio Ragagnin
  • 2,278
  • 4
  • 24
  • 39
1
vote
1 answer

Dynamically defining methods in a loop, to emulate numeric object, but all methods receive value of last iteration, why?

I'm trying to dynamically set methods to emulate a numeric object with the code below. But instead, every method is set to the last one in the loop. Why does this happen and how can I convince Python to DWIM? #!/usr/bin/env python3.5 class…
gerrit
  • 24,025
  • 17
  • 97
  • 170
1
vote
1 answer

Python: How to implement polymorphic binary op. magic methods?

Context: I use Pandas on a daily basis (processing measurement data) and I'd like learn more about Python. To do so, I`m working on a (wrapper) class --- MyDataFrame --- that combines Pandas DataFrame functionality with that of Pint --- a Python…
balletpiraat
  • 206
  • 1
  • 11