diff options
author | andrewgsavage <andrewgsavage@gmail.com> | 2021-03-01 23:45:52 +0000 |
---|---|---|
committer | andrewgsavage <andrewgsavage@gmail.com> | 2021-03-01 23:45:52 +0000 |
commit | 1b64963dae9aad4509c40bfb6f749070ea4c73d4 (patch) | |
tree | 484a6807580d3da86454a24ce750a5e9916312a1 /docs | |
parent | 9f412c1ad7af3a70ee2d3bf85af350c3123e2008 (diff) | |
download | pint-1b64963dae9aad4509c40bfb6f749070ea4c73d4.tar.gz |
plotting
Diffstat (limited to 'docs')
-rw-r--r-- | docs/pint-pandas.ipynb | 104 |
1 files changed, 87 insertions, 17 deletions
diff --git a/docs/pint-pandas.ipynb b/docs/pint-pandas.ipynb index 7b9e1ef..b4e7bc7 100644 --- a/docs/pint-pandas.ipynb +++ b/docs/pint-pandas.ipynb @@ -43,7 +43,9 @@ "cell_type": "markdown", "metadata": {}, "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" + "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)" ] }, { @@ -53,8 +55,7 @@ "outputs": [], "source": [ "import pandas as pd \n", - "import pint\n", - "import pint_pandas", + "import pint" ] }, { @@ -217,12 +218,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'''" ] }, { @@ -239,7 +241,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" ] @@ -274,7 +276,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As previously, operations between DataFrame columns are unit aware" + "Let's confirm the units have been parsed correctly" ] }, { @@ -283,7 +285,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." ] }, { @@ -292,7 +301,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" ] }, { @@ -301,8 +318,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_" ] }, @@ -335,8 +361,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()" ] }, @@ -377,6 +404,49 @@ "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": {}, + "source": [ "## Advanced example\n", "This example shows alternative ways to use pint with pandas and other features.\n", "\n", |