Skip to content

Commit

Permalink
Merge pull request #1254 from andrewgsavage/plotting_pintpandas
Browse files Browse the repository at this point in the history
Plotting pintpandas
  • Loading branch information
hgrecco authored Mar 3, 2021
2 parents 2e1185c + 0ea5b90 commit 3cf3a81
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 29 deletions.
102 changes: 86 additions & 16 deletions docs/pint-pandas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"source": [
"This example will show the simplist way to use pandas with pint and the underlying objects. It's slightly fiddly as you are not reading from a file. A more normal use case is given in Reading a csv.\n",
"\n",
"First some imports (you don't need to import `pint_pandas` for this to work)"
"First some imports"
]
},
{
Expand All @@ -55,7 +55,8 @@
"outputs": [],
"source": [
"import pandas as pd \n",
"import pint"
"import pint\n",
"import pint_pandas",
]
},
{
Expand Down Expand Up @@ -218,12 +219,13 @@
"metadata": {},
"outputs": [],
"source": [
"test_data = '''speed,mech power,torque,rail pressure,fuel flow rate,fluid power\n",
"rpm,kW,N m,bar,l/min,kW\n",
"1000.0,,10.0,1000.0,10.0,\n",
"1100.0,,10.0,100000000.0,10.0,\n",
"1200.0,,10.0,1000.0,10.0,\n",
"1200.0,,10.0,1000.0,10.0,'''"
"test_data = '''ShaftSpeedIndex,rpm,1200,1200,1200,1600,1600,1600,2300,2300,2300\n",
"pump,,A,B,C,A,B,C,A,B,C\n",
"ShaftSpeed,rpm,1200,1200,1200,1600,1600,1600,2300,2300,2300\n",
"FlowRate,m^3 h^-1,8.72,9.28,9.31,11.61,12.78,13.51,18.32,17.90,19.23\n",
"DifferentialPressure,kPa,162.03,144.16,136.47,286.86,241.41,204.21,533.17,526.74,440.76\n",
"ShaftPower,kW,1.32,1.23,1.18,3.09,2.78,2.50,8.59,8.51,7.61\n",
"Efficiency,dimensionless,30.60,31.16,30.70,30.72,31.83,31.81,32.52,31.67,32.05'''"
]
},
{
Expand All @@ -240,7 +242,7 @@
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(io.StringIO(test_data), header=[0, 1])\n",
"df = pd.read_csv(io.StringIO(test_data), header=[0, 1], index_col = [0,1]).T\n",
"# df = pd.read_csv(\"/path/to/test_data.csv\", header=[0, 1])\n",
"df"
]
Expand Down Expand Up @@ -275,7 +277,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"As previously, operations between DataFrame columns are unit aware"
"Let's confirm the units have been parsed correctly"
]
},
{
Expand All @@ -284,7 +286,14 @@
"metadata": {},
"outputs": [],
"source": [
"df_.speed * df_.torque"
"df_.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here the h in m^3 h^-1 has been parsed as the planck constant. Let's change the unit to hours."
]
},
{
Expand All @@ -293,7 +302,15 @@
"metadata": {},
"outputs": [],
"source": [
"df_"
"df_['FlowRate'] = pint_pandas.PintArray(df_['FlowRate'].values.quantity.m, dtype = \"pint[m^3/hr]\")\n",
"df_.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As previously, operations between DataFrame columns are unit aware"
]
},
{
Expand All @@ -302,8 +319,17 @@
"metadata": {},
"outputs": [],
"source": [
"df_['mech power'] = df_.speed * df_.torque\n",
"df_['fluid power'] = df_['fuel flow rate'] * df_['rail pressure']\n",
"df_.ShaftPower / df_.ShaftSpeed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_['ShaftTorque'] = df_.ShaftPower / df_.ShaftSpeed\n",
"df_['FluidPower'] = df_['FlowRate'] * df_['DifferentialPressure']\n",
"df_"
]
},
Expand Down Expand Up @@ -336,8 +362,9 @@
"metadata": {},
"outputs": [],
"source": [
"df_['fluid power'] = df_['fluid power'].pint.to(\"kW\")\n",
"df_['mech power'] = df_['mech power'].pint.to(\"kW\")\n",
"df_['FluidPower'] = df_['FluidPower'].pint.to(\"kW\")\n",
"df_['FlowRate'] = df_['FlowRate'].pint.to(\"L/s\")\n",
"df_['ShaftTorque'] = df_['ShaftTorque'].pint.to(\"N m\")\n",
"df_.pint.dequantify()"
]
},
Expand Down Expand Up @@ -374,6 +401,49 @@
"df_.pint.to_base_units().pint.dequantify()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Plotting\n",
"Pint's matplotlib support allows columns with the same dimensionality to be plotted."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pint_pandas.PintType.ureg.setup_matplotlib()\n",
"ax = df_[['ShaftPower', 'FluidPower']].unstack(\"pump\").plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ax.yaxis.units"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that indexes cannot store PintArrays, so don't contain unit information"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(ax.xaxis.units)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
13 changes: 0 additions & 13 deletions pint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
:license: BSD, see LICENSE for more details.
"""

import sys

from .context import Context
from .errors import ( # noqa: F401
DefinitionSyntaxError,
Expand All @@ -36,17 +34,6 @@
# Backport for Python < 3.8
from importlib_metadata import version

try:
from pint_pandas import PintArray, PintType

del PintType
del PintArray

_HAS_PINT_PANDAS = True
except ImportError:
_HAS_PINT_PANDAS = False
_, _pint_pandas_error, _ = sys.exc_info()

try: # pragma: no cover
__version__ = version("pint")
except Exception: # pragma: no cover
Expand Down

0 comments on commit 3cf3a81

Please sign in to comment.