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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
15 views

Get unit and not base unit

I am trying to get a more meaningful unit name when converting between unit systems. In a separate file (custom_en.txt), I have defined a new unit system: @system Nmm using international ...
Rufus's user avatar
  • 15
0 votes
1 answer
24 views

python pint : Create a "pace" metric from velocity metrics

Using the pint library in python - It has support for unit conversions, which work great. For example: units.Quantity(2, "mph").to("kph") I would like to understand how I could ...
time4tea's user avatar
  • 2,177
0 votes
1 answer
34 views

Default to a derived unit on output in python pint

Is it possible to default the output if a unit with pint to a derived unit? For example, if I compute a pressure by dividing a force and area, I would like it to display in "psi" by default. ...
slaughter98's user avatar
  • 1,791
0 votes
0 answers
37 views

How to set field `pint.Quantity._magnitude` of existing object?

Using the pint library, I profiled my code and found a bottleneck is creating new Quantity objects using the constructor like this: import pint ureg = pint.UnitRegistry() ... quantity = pint.Quantity(-...
Dave Doty's user avatar
  • 315
0 votes
0 answers
40 views

How to override predefined unit in pint?

I use the pint library in my lab assignments to efficiently deal with unit specifications and conversions. We recently had an experiment when we took some pictures and in the code, we need to convert ...
Shai Avr's user avatar
  • 1,022
0 votes
0 answers
49 views

Can Pint handle complex calculation when defining new Units?

This is the first time I'm creating my own units in the Pint registry. I'm trying to custom define sucrose units. Mostly density. Can Pint handle more complex calculations than just basic math? It'...
Aaron Paxson's user avatar
2 votes
2 answers
241 views

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....
Dunbur's user avatar
  • 23
1 vote
1 answer
842 views

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 ...
Shaya Ulman's user avatar
  • 1,347
3 votes
1 answer
125 views

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 ...
Selewirre's user avatar
0 votes
0 answers
23 views

Pint Quantity Console Outputs Stuck in Latex Mode

I have been using pint for a little while now. At some point I did something to get fancy outputs in my console, I think from a LaTex printer or something similar. Now I cannot figure out how to ...
Nate Solnit's user avatar
0 votes
0 answers
65 views

Pint support for saving to Parquet, hdf5 and Feather files

Pandas dataframes with Pint dtypes do not appear to be saving to Parquet or Hdf5 format. Is there no support for this, or am I doing this wrong. import pandas as pd import numpy as np import pint,...
kbk78's user avatar
  • 151
0 votes
0 answers
21 views

How can I reinterpret a non-multiplicative Unit in a Quantity to its delta variant, regardless of whether it is in a multiplicative context?

The context for this question is that, using pint to parse input from users, I don't want those users to have to worry about delta vs non-delta units. I also know, for each quantity, whether it ...
cge's user avatar
  • 9,631
1 vote
1 answer
117 views

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 ...
Riley Brady's user avatar
1 vote
1 answer
347 views

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 */ ...
mstdmstd's user avatar
  • 2,423
1 vote
2 answers
702 views

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?',...
mstdmstd's user avatar
  • 2,423
2 votes
2 answers
160 views

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) ...
cuzureau's user avatar
  • 350
0 votes
1 answer
398 views

Typing and pint

I'm using pint to use and convert units. I wanted to create classes which restricts the quantities only to "[time]" or "[length]" dimensions, so as a first approach I did the ...
JLDiaz's user avatar
  • 1,288
0 votes
1 answer
263 views

Pint: How to convert units in one dimension, leaving other dimensions unchanged?

I'd like to normalize measurements of different timespans to per-annum: import pint from pint import get_application_registry ureg = get_application_registry() x = ureg('kg/d') print(f"x = {x}&...
Michael Tiemann's user avatar
0 votes
1 answer
947 views

Spectrochempy Unable to Find "pint.unit" -- Module Not Found Error

I am trying to install spectrochempy (https://www.spectrochempy.fr/stable/gettingstarted/install/install_win.html) via conda on Windows 10. I am able to follow the instructions without an error ...
Pawel's user avatar
  • 21
0 votes
1 answer
514 views

pint-pandas import error ModuleNotFoundError: No module named 'pint.quantity' (pint is imported)

I am trying to use pint-pandas, but it errors on import with a ModuleNotFoundError: No module named 'pint.quantity'. MRE from the pint-pandas github 'basic example' (because it errors on import ...
Robbes's user avatar
  • 135
0 votes
1 answer
50 views

Solving simultaneous nonlinear equations with units

I am basically building a general tool for solving for the operating point of a pump in a system - i.e., two equations and two unknowns - pump curve/system curve and head (H) and flowrate (Q). My ...
nate's user avatar
  • 269
1 vote
1 answer
91 views

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 ...
mrclary's user avatar
  • 489
2 votes
1 answer
119 views

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: ...
Michal Kaut's user avatar
  • 1,453
0 votes
1 answer
153 views

How to serialize metpy (pint) units for use with dask distributed?

How can I properly serialize metpy units (based on pint) to work with dask distributed? As far as I understand, it looks like dask distributed automatically pickles data for ease of transfer, but ...
bwc's user avatar
  • 1,047
0 votes
1 answer
460 views

How are currency units used with Python Pint units?

I would like to do something like this: import pint ureg = pint.UnitRegistry() kg = ureg.kg USD = ureg.USD # not the way to do this weight = 2.3 * kg price = 1.49 * USD / kg cost = weight * price ...
Barry Andersen's user avatar
0 votes
1 answer
41 views

Need to install Python Pint Package using MSYS

I am very new to using MSYS and I need it to run a Fortran based code through python interface. I followed the whole installation procedure in this link https://github.com/SINTEF/thermopack/blob/main/...
Ali Marzooq's user avatar
1 vote
1 answer
465 views

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 ...
Malcolm Ross's user avatar
1 vote
1 answer
38 views

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]"),...
Matt's user avatar
  • 1,958
1 vote
1 answer
273 views

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'...
mkais's user avatar
  • 11
0 votes
1 answer
173 views

pint: electron_volt as base unit

Is there any way to have the composed unit eV as a base unit in pint using a straight forward combination of @system, @group and possibly some contextual conversions in a definition file? What I want ...
red-isso's user avatar
  • 315
0 votes
1 answer
95 views

pint: Is wrapping built-in functions with ureg.wraps() impossible?

I'm trying to wrap time.sleep: from time import sleep from pint import UnitRegistry ureg = UnitRegistry() ureg.wraps(None, ureg.seconds)(sleep) However, this code throws a ValueError: Traceback (...
Sören's user avatar
  • 1,990
2 votes
1 answer
5k views

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 ...
SiamSaleh's user avatar
  • 480
0 votes
1 answer
259 views

KeyError with custom derived quantity

I have defined a new derived dimension with [molar_energy] = [energy] / [substance] However, if I do the following it complains: >>> UR.get_compatible_units('[molar_energy]') Traceback (most ...
Rufus's user avatar
  • 15
3 votes
1 answer
525 views

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 [...
ElRudi's user avatar
  • 2,182
0 votes
1 answer
235 views

In `pint`, set per-unit `default_format`

Basically the title. In pint, is there a way to define the default string formatting per dimension or per unit, instead of 'across the board'? Stated more precisely: I want to format a quantity's ...
ElRudi's user avatar
  • 2,182
0 votes
2 answers
459 views

pint: convert geographic CRS degrees to nautical miles

I would like to use pint to convert degrees (distance in a geographic CRS) into nautical miles. https://geopandas.org/docs/reference/api/geopandas.GeoDataFrame.sjoin_nearest.html outputs distance in ...
Ray Bell's user avatar
  • 1,528
3 votes
2 answers
445 views

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 ...
drhagen's user avatar
  • 8,560
1 vote
2 answers
707 views

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 ...
mmj's user avatar
  • 5,574
3 votes
1 answer
526 views

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 = ...
drhagen's user avatar
  • 8,560
1 vote
1 answer
554 views

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 ...
Jennifer's user avatar
1 vote
1 answer
227 views

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 ...
Dimitri Coukos's user avatar
-1 votes
1 answer
193 views

MetPy vs Pint package compatibility error

In my Conda environment, when I run pip show MetPy, my first and last two lines are: Name: MetPy Version: 1.0 ... Requires: scipy, matplotlib, pandas, pyproj, pooch, importlib-resources, traitlets, ...
Stephen's user avatar
  • 19
4 votes
3 answers
1k views

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 ...
Jean-François Corbett's user avatar
0 votes
1 answer
817 views

How to print python Pint quantities and its meaning

I am very new to python and python pint. I just want to know the supportable units of python pint. Also what is the meaning of the below representation: {length: 1, time: -1} Reference from pint ...
june alex's user avatar
  • 254
1 vote
1 answer
251 views

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 = ...
Jean-François Corbett's user avatar
0 votes
0 answers
205 views

Does anyone know a better way to evaluate dimension of the formula in python?

Now, I have a formula with some symbols. These symbols have units. Does anyone know a python library to evaluate the dimension of the formula. I checked sympy and pint. Sympy seems to be based on the ...
TaskeOnTheBeach's user avatar
1 vote
0 answers
137 views

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 ...
JeffP's user avatar
  • 324
2 votes
1 answer
201 views

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_ = ...
Ludwiggle's user avatar
0 votes
1 answer
105 views

How can you define a 2-D Quantity Array in Python with different units in each dimension

I want to define an array containing fuel consumption data for a genset. The x-axis will have units of 'kW' and the y-axis 'gal/hr'. Is there a way to create a Numpy compatible Quantity Array with ...
Darko's user avatar
  • 599
0 votes
1 answer
28 views

Dealing with an error without defining what the correct answer is [duplicate]

I'd like to deal with an input error without defining what the success criteria is i.e. only loop back round the user input turns out to be incorrect. All of the examples I can find require a ...
KennyMcK's user avatar