'Pandas+Uncertainties producing AttributeError: type object 'dtype' has no attribute 'kind'

I want to use Pandas + Uncertainties. I am getting a strange error, below a MWE:

from uncertainties import ufloat
import pandas

number_with_uncertainty = ufloat(2,1)

df = pandas.DataFrame({'a': [number_with_uncertainty]}) # This line works fine.

df.loc[0,'b'] = ufloat(3,1) # This line fails.

I have noticed that if I try to add the ufloats "on the fly", as I usually do with a float or some other stuff, it fails. If I first create a Series then it works:

from uncertainties import ufloat
import pandas

number_with_uncertainty = ufloat(2,1)

df = pandas.DataFrame({'a': [number_with_uncertainty]}) # This line works fine.

df['b'] = pandas.Series([ufloat(3,1)]) # Now it works.

print(df)

This makes it more cumbersome when calculating values on the fly within a loop as I have to create a temporary Series and after the loop add it as a column into my data frame.

Is this a problem of Pandas, a problem of Uncertainties, or am I doing something that is not supposed to be done?



Solution 1:[1]

The problem arises because when pandas tries to create a new column it checks the dtype of the new value so that it knows what dtype to assign to that column. For some reason, the dtype check on the ufloat value fails. I believe this is a bug that will have to be fixed in uncertainties.

A workaround in the interim is to manually create the new column with dtype set to object, for example in your case above:

from uncertainties import ufloat
import pandas
import numpy

number_with_uncertainty = ufloat(2,1)

df = pandas.DataFrame({'a': [number_with_uncertainty]}) # This line works fine.

# create a new column with the correct dtype
df.loc[:, 'b'] = numpy.zeros(len(df), dtype=object)

df.loc[0,'b'] = ufloat(3,1) # This line now works.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 oscarbranson