Pint-Pandas
Pint-Pandas is a Python library that extends Pandas DataFrames and Series to seamlessly integrate physical quantities with units, leveraging the Pint library. It enables units-aware operations directly within Pandas data structures, ensuring dimensional consistency and preventing common unit errors. The library is actively maintained, with version 0.8.0 released on March 19, 2026. It's important to note that the Pandas integration is still considered experimental by the underlying Pint project.
Common errors
-
ModuleNotFoundError: No module named 'pint_pandas'
cause The `pint-pandas` package has not been installed. Users sometimes install `pint` but forget to install its Pandas integration package.fixRun `pip install pint-pandas` or `conda install -c conda-forge pint-pandas` to install the package. -
DataFrame `dtype` is `object` instead of `pint[unit]` for columns expected to have units.
cause Columns were created or loaded (e.g., from CSV) containing `pint.Quantity` objects but were not explicitly cast to `PintArray` dtypes, leading Pandas to infer an `object` dtype. Operations may then fail to be units-aware.fixAfter loading data, use the `.pint.quantify()` or `.pint.convert_object_dtype()` accessor methods to correctly convert the `object` columns into `PintArray` dtypes. For example: `df = df.pint.quantify(level=-1)` if units are in the header, or `df = df.pint.convert_object_dtype()` for Series of Quantities. -
In-place Series operations (e.g., `s.pint.ito('new_unit')`) appear to do nothing.cause Due to how Pint-Pandas handles delegated methods for its Series accessor, direct in-place modification might not work as intuitively expected for all methods.fixInstead of in-place operations, reassign the result of the method. For example, use `s = s.pint.to('new_unit')` instead of `s.pint.ito('new_unit')`.
Warnings
- gotcha The Pandas integration is officially considered 'experimental'. While functional, some edge cases or advanced Pandas features might not behave as expected or could change in future releases.
- gotcha When downcasting a PintArray to a NumPy array (e.g., implicitly in some Pandas operations), the unit information will be stripped, resulting in a `UnitStrippedWarning`. This can lead to loss of unit context if not handled explicitly.
- breaking Pint-Pandas has specific minimum version requirements for both `pandas` and `pint` that evolve with new releases. For instance, `pint-pandas` 0.6 requires `pandas` >= 2.2 and `pint` >= 0.24. Pandas 3.0 also introduced significant breaking changes and mandates Python 3.11+.
Install
-
pip install pint-pandas -
conda install -c conda-forge pint-pandas
Imports
- pandas
import pandas as pd
- pint
import pint
- pint_pandas
from pint import pandas
import pint_pandas
Quickstart
import pandas as pd
import pint
import pint_pandas
# Create a DataFrame with PintArrays as columns
df = pd.DataFrame({
"torque": pd.Series([1.0, 2.0, 2.0, 3.0], dtype="pint[lbf ft]"),
"angular_velocity": pd.Series([1.0, 2.0, 2.0, 3.0], dtype="pint[rpm]"),
})
print("Original DataFrame:\n", df)
print("\nData Types:\n", df.dtypes)
# Perform units-aware operations
df['power'] = df['torque'] * df['angular_velocity']
print("\nDataFrame with calculated 'power':\n", df)
print("\nData Types after calculation:\n", df.dtypes)
# Access magnitudes or convert units
print("\nPower in Watts:\n", df['power'].pint.to('W'))