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
MgO SiO2 FeO CaO Na2O Te K Na
0 8.629 41.856 6.018 5.139 1.929 1556.009 1109.907 976.978
1 9.025 37.087 5.852 4.902 1.944 1987.122 1157.703 974.231


As the units are heterogeneous, we’ll need to convert the data frame to a single set of units (here we use 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, :]
MgO SiO2 FeO Ca Te Na Na/Te MgO/SiO2
0 8.629 41.856 6.018 3.673 0.156 1.529 9.827 0.206
1 9.025 37.087 5.852 3.503 0.199 1.539 7.746 0.243


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, :]
FeO Fe2O3
0 5.416 0.669
1 5.267 0.650


Total running time of the script: (0 minutes 0.147 seconds)