Element-Oxide Transformation
One of pyrolite’s strengths is converting mixed elemental and oxide data to a new
form. The simplest way to perform this is by using the
convert_chemistry()
function. Note that by default
pyrolite assumes that data are in the same units.
import pandas as pd
import pyrolite.geochem
pd.set_option("display.precision", 3) # smaller outputs
Here we create some synthetic data to work with, which has some variables in Wt% and some in ppm. Notably some elements are present in more than one column (Ca, Na):
from pyrolite.util.synthetic import normal_frame
df = normal_frame(columns=["MgO", "SiO2", "FeO", "CaO", "Na2O", "Te", "K", "Na"]) * 100
df.pyrochem.elements *= 100 # elements in ppm
df.head(2)
As the units are heterogeneous, we’ll need to convert the data frame to a single set of units (here we use Wt%):
df.pyrochem.elements = df.pyrochem.elements.pyrochem.scale("ppm", "wt%") # ppm to wt%
We can transform this chemical data to a new set of compositional variables. Here we i) convert CaO to Ca, ii) aggregate Na2O and Na to Na and iii) calculate mass ratios for Na/Te and MgO/SiO2. Note that you can also use this function to calculate mass ratios:
df.pyrochem.convert_chemistry(
to=["MgO", "SiO2", "FeO", "Ca", "Te", "Na", "Na/Te", "MgO/SiO2"]
).head(2)
/home/docs/checkouts/readthedocs.org/user_builds/pyrolite/checkouts/main/pyrolite/geochem/transform.py:351: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[3.67297884 3.50325632 3.66224102 3.25764225 3.52937765 3.9315398
3.84124843 3.39657993 4.14447134 3.84324796]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
_df.loc[:, targetnames] = subsum.values[:, np.newaxis] @ coeff[np.newaxis, :]
You can also specify molar ratios for iron redox, which will result in multiple iron species within the single dataframe:
df.pyrochem.convert_chemistry(to=[{"FeO": 0.9, "Fe2O3": 0.1}]).head(2)
/home/docs/checkouts/readthedocs.org/user_builds/pyrolite/checkouts/main/pyrolite/geochem/transform.py:351: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '[0.66875538 0.6503792 0.68473922 0.63454072 0.59756727 0.67044164
0.69027087 0.70640098 0.70099976 0.66848851]' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
_df.loc[:, targetnames] = subsum.values[:, np.newaxis] @ coeff[np.newaxis, :]
Total running time of the script: (0 minutes 0.147 seconds)