-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3445aab
commit 948392f
Showing
9 changed files
with
152 additions
and
113 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,4 @@ That's all! You can check that Pint is correctly installed by starting up python | |
:hidden: | ||
|
||
tutorial | ||
faq | ||
projects |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
***************************** | ||
Pint-Pandas in your projects | ||
***************************** | ||
|
||
Using a Shared Unit Registry | ||
---------------------------- | ||
|
||
As described `in the documentation of the main pint package: <https://pint.readthedocs.io/en/stable/getting/pint-in-your-projects.html#using-pint-in-your-projects>`_: | ||
|
||
If you use Pint in multiple modules within your Python package, you normally want to avoid creating multiple instances of the unit registry. The best way to do this is by instantiating the registry in a single place. For example, you can add the following code to your package ``__init__.py`` | ||
|
||
When using `pint_pandas`, this extends to using the same unit registry that was created by the main `pint` package. This is done by using the :func:`pint.get_application_registry() <pint:get_application_registry>` function. | ||
|
||
In a sample project structure of this kind: | ||
|
||
.. code-block:: text | ||
. | ||
└── mypackage/ | ||
├── __init__.py | ||
├── main.py | ||
└── mysubmodule/ | ||
├── __init__.py | ||
└── calculations.py | ||
After defining the registry in the ``mypackage.__init__`` module: | ||
|
||
.. code-block:: python | ||
from pint import UnitRegistry, set_application_registry | ||
ureg = UnitRegistry() | ||
ureg.formatter.default_format = "P" | ||
set_application_registry(ureg) | ||
In the ``mypackage.mysubmodule.calculations`` module, you should *get* the shared registry like so: | ||
|
||
.. code-block:: python | ||
import pint | ||
ureg = pint.get_application_registry() | ||
@ureg.check( | ||
'[length]', | ||
) | ||
def multiply_value(distance): | ||
return distance * 2 | ||
Failure to use the application registry will result in a ``DimensionalityError`` of the kind: | ||
|
||
Cannot convert from '<VALUE> <UNIT>' ([<DIMENSION>]) to 'a quantity of' ([<DIMENSION>])". | ||
|
||
For example: | ||
|
||
.. code-block:: text | ||
DimensionalityError: Cannot convert from '200 metric_ton' ([mass]) to 'a quantity of' ([mass])" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ examples that describe many common tasks that you can accomplish with pint. | |
|
||
reading | ||
initializing | ||
numpy | ||
common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
.. _numpy: | ||
|
||
************************** | ||
Numpy support | ||
************************** | ||
|
||
Numpy functions that work on pint ``Quantity`` ``ndarray`` objects also work on ``PintArray``. | ||
|
||
|
||
.. ipython:: python | ||
:suppress: | ||
import pandas as pd | ||
import pint | ||
import pint_pandas | ||
import numpy as np | ||
PintArray = pint_pandas.PintArray | ||
ureg = pint_pandas.PintType.ureg | ||
Quantity = ureg.Quantity | ||
.. ipython:: python | ||
pa = PintArray([1, 2, np.nan, 4, 10], dtype="pint[m]") | ||
np.clip(pa, 3 * ureg.m, 5 * ureg.m) | ||
Note that this function errors when applied to a ``Series``. | ||
|
||
.. ipython:: python | ||
:okexcept: | ||
df = pd.DataFrame({"A": pa}) | ||
np.clip(df['A'], 3 * ureg.m, 5 * ureg.m) | ||
Apply the function to the ``PintArray`` instead of the ``Series`` using ``Series.values``. | ||
|
||
.. ipython:: python | ||
:okexcept: | ||
np.clip(df['A'].values, 3 * ureg.m, 5 * ureg.m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ sphinx-book-theme>=1.1.0 | |
sphinx_copybutton | ||
sphinx_design | ||
typing_extensions | ||
uncertainties-pandas |