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
2
votes
2 answers

How can I parse the unit : "g/100mL" using unit-parse in Python?

I'm trying to parse strings in Python, looking for scientific values and units. I want to retrieve them in order to convert them to some other units. I'm using the library unit-parse (based on pint) but it has trouble understanding this example :…
cuzureau
  • 330
  • 2
  • 17
2
votes
1 answer

How to use transformations in pint package correctly?

I am working on a project involving natural gas and want to be able to convert between volume, mass and energy using provided densities. I have the following code python, using the pint package: import pint ureg =…
Michal Kaut
  • 1,421
  • 1
  • 14
  • 25
2
votes
1 answer

ModuleNotFoundError: No module named 'importlib_metadata' while installing pint in python

I have tried installing pint using pip. But whenever I am trying, I am getting ModuleNotFoundError: No module named 'importlib_metadata'. Traceback: ModuleNotFoundError Traceback (most recent call…
SiamSaleh
  • 460
  • 6
  • 12
2
votes
1 answer

Ending up with both meters and kilometers using pint in python

Using the pint in code below I somehow end up with both units: meter and kilometer: 107661.59454231549 meter ** 1.5 / kilometer ** 0.5 / second import math import pint u = pint.UnitRegistry() Q_ = u.Quantity R_mars = 3396 * u.km m_mars = 6.419 *…
Ludwiggle
  • 23
  • 3
2
votes
1 answer

Python: Pandas support in Pint

I am trying to reproduce the example described in the Python Pint library here. In the section "Reading from csv" when running the following line: df_ = df.pint.quantify(level=-1) I got the following message error: AttributeError: 'DataFrame'…
Mathador
  • 144
  • 1
  • 1
  • 11
2
votes
1 answer

Why does pint need a unit registry?

The default example for pint is >>> import pint >>> ureg = pint.UnitRegistry() >>> 3 * ureg.meter + 4 * ureg.cm which makes me wonder what I need the unit registry object for. For example, I could imagine it to be just a…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
2
votes
2 answers

Unable to decorate a class method using Pint units

Here's a very simple example in an effort to decorate a class method using Pint, from pint import UnitRegistry ureg = UnitRegistry() Q_ = ureg.Quantity class Simple: def __init__(self): pass @ureg.wraps('m/s', (None, 'm/s'), True) def…
aaron02
  • 320
  • 2
  • 14
2
votes
3 answers

how do I make a numpy array from a list of pint quantities

import numpy as np from pint import UnitRegistry unit = UnitRegistry() Q_ = unit.Quantity a = 1.0*unit.meter b = 2.0*unit.meter # some calculations that change a and b x=np.array([a.magnitude,b.magnitude])*Q_(1.0,a.units) will make a numpy array…
pheon
  • 2,867
  • 3
  • 26
  • 33
1
vote
1 answer

How to automatically run Laravel Pint on file save in VSCode?

The Laravel Pint docs specifies that you can run Pint by invoking the binary located in the vendor/bin directory like this: ./vendor/bin/pint I would like to set this up in VSCode so that it automatically runs Pint when a specific file is saved.…
Shaya Ulman
  • 1,299
  • 1
  • 12
  • 24
1
vote
1 answer

Difficulty with custom conversion factor using contexts with pint

I am looking to set up a fixed conversion factor between two specific units that cause a DimensionalityError in pint. I have daily accumulated solar radiation data that is in J/m2 and want to convert it into W/m2. Since this is a daily accumulation,…
1
vote
1 answer

How with pint to remove empty line in class declaration?

Which property have I to set from https://github.com/laravel/pint/blob/main/resources/presets/laravel.php rules to remove empty line in class declaration : class Classname { /** @var int */ protected $currentLocate = 'ua'; /** @var…
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
1
vote
2 answers

How with laravel/pint to make keys and values are aligned with spaces on the same level?

In Laravel 9 app using laravel/pint (1.4) with "psr12" preset I prefer to see not code : \DB::table('quizzes')->insert([ 'id' => 2, 'question' => 'What does ORM stand for?', 'quiz_category_id' => 1, 'points' => 4, 'active' =>…
mstdmstd
  • 2,195
  • 17
  • 63
  • 140
1
vote
1 answer

Python pint objects passed to multiprocessing have different registries than objects created in process

I have an application that requires multiprocessing where pint objects are passed as arguments to the process and pint objects are also created in the process. But these different pint objects have different registries from each other. I expect the…
mrclary
  • 489
  • 3
  • 16
1
vote
1 answer

Pint: How do I define my own units in the Pint Python library?

I am working with Pint to do unit conversions in a Python project. The project involves temperature gradients, which are not defined in Pint. My units are typically "deg.C/km" so I'd like to be able to declare a Quantity as "55 deg.C/km". I'd…
1
vote
1 answer

Dataframe zeros out quantities when a date time is used for an index

Whenever set my DataFrame index to a list of datetime's, the Dataframe zeros out all my quantities. df = pd.DataFrame( { "code": pd.Series(mem_code, dtype="pint[byte]"), "data": pd.Series(mem_data, dtype="pint[byte]"), …
Matt
  • 1,928
  • 24
  • 44