Initializing data#

There are several ways to initialize PintArrays in a DataFrame. Here’s the most common methods. We’ll use PA_ and Q_ as shorthand for PintArray and Quantity.

In [1]: import pandas as pd

In [2]: import pint

In [3]: import pint_pandas

In [4]: import io

In [5]: PA_ = pint_pandas.PintArray

In [6]: ureg = pint_pandas.PintType.ureg

In [7]: Q_ = ureg.Quantity

In [8]: df = pd.DataFrame(
   ...:     {
   ...:         "A": pd.Series([1.0, 2.0], dtype="pint[m]"),
   ...:         "B": pd.Series([1.0, 2.0]).astype("pint[m]"),
   ...:         "C": PA_([2.0, 3.0], dtype="pint[m]"),
   ...:         "D": PA_([2.0, 3.0], dtype="m"),
   ...:         "E": PA_([2.0, 3.0], dtype=ureg.m),
   ...:         "F": PA_.from_1darray_quantity(Q_([2, 3], ureg.m)),
   ...:         "G": PA_(Q_([2.0, 3.0], ureg.m)),
   ...:     }
   ...: )
   ...: 

In [9]: df
Out[9]: 
     A    B    C    D    E  F    G
0  1.0  1.0  2.0  2.0  2.0  2  2.0
1  2.0  2.0  3.0  3.0  3.0  3  3.0

Note

“pint[unit]” must be used for the Series or DataFrame constuctor.