pythonpandasuncertainty

Extract nominal and standard deviation from ufloat inside a panda dataframe


For convenience purpose I am using pandas dataframes in order to perform an uncertainty propagation on a large set on data.

I then wish to plot the nominal value of my data set but something like myDF['colLabel'].n won't work. How to extract the nominal and standard deviation from a dataframe in order to plot the nominal value and the errorbar?

Here is a MWE to be more consistent:

#%% MWE
import pandas as pd
from uncertainties import ufloat
import matplotlib.pyplot as plt

# building of a dataframe filled with ufloats
d = {'value1': [ufloat(1,.1),ufloat(3,.2),ufloat(5,.6),ufloat(8,.2)], 'value2': [ufloat(10,5),ufloat(50,2),ufloat(30,3),ufloat(5,1)]}
df = pd.DataFrame(data = d)

# plot of value2 vs. value1 with errobars.
plt.plot(x = df['value1'].n, y = df['value2'].n)
plt.errorbar(x = df['value1'].n, y = df['value2'].n, xerr = df['value1'].s, yerr = df['value2'].s)
# obviously .n and .s won't work.

I get as an error AttributeError: 'Series' object has no attribute 'n' which suggest to extract the values from each series, is there a shorter way to do it than going through a loop which would separate the nominal and std values into two separated vectors?

Thanks.

EDIT: Using those functions from the package won't work either: uncertainties.nominal_value(df['value2']) and uncertainties.std_dev(df['value2'])


Solution

  • Actually solved it with the unumpy.nominal_values(arr) and unumpy.std_devs(arr) functions from uncertainties.