Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with 'uncertainties'? #124

Closed
Cs137 opened this issue Apr 30, 2022 · 8 comments
Closed

Compatibility with 'uncertainties'? #124

Cs137 opened this issue Apr 30, 2022 · 8 comments

Comments

@Cs137
Copy link

Cs137 commented Apr 30, 2022

Many thanks for your work on this package and especially for pint itself! It makes life much easier, and in order to make life more easy, I would like to request a feature.

In my opinion the usage of the "pint" dtype for a column containing an uarray would be very handy, but there seems to be an issue with the __init__ method of PintArray. If I get it correctly, it creates a np.array from the provided data and thus wants to convert the uarray into floats, which causes the following error (I shortened the path and inserted [...]):

File [...]/python3.10/site-packages/pint_pandas/pint_array.py:198, in PintArray.__init__(self, values, dtype, copy)
    193     data_dtype = next(x for x in values if not isinstance(x, float))
    194     warnings.warn(
    195         f"pint-pandas does not support magnitudes of {type(data_dtype)}. Converting magnitudes to float.",
    196         category=RuntimeWarning,
    197     )
--> 198 self._data = np.array(values, float, copy=copy)
    199 self._Q = self.dtype.ureg.Quantity

File [...]/python3.10/site-packages/uncertainties/core.py:2712, in add_operators_to_AffineScalarFunc.<locals>.raise_error(self)
   2711 def raise_error(self):
-> 2712     raise TypeError("can't convert an affine function (%s)"
   2713                     ' to %s; use x.nominal_value'
   2714                     # In case AffineScalarFunc is sub-classed:
   2715                     % (self.__class__, coercion_type))

TypeError: can't convert an affine function (<class 'uncertainties.core.Variable'>) to float; use x.nominal_value

Here some snippets to reproduce the case:

import pandas as pd
import pint
import pint_pandas
from uncertainties import ufloat_fromstr, unumpy

data = {'a': {0: 0.01, 1: 0.28, 2: 0.33, 3: 0.78}, 
        'b': {0: '0.79+/-0.08', 1: '0.340+/-0.030', 
              2: '0.52+/-0.05', 3: '0.250+/-0.020'}}

df = pd.DataFrame(data)
df.b = [ufloat_fromstr(x) for x in df.b]

# dtype assignment works fine for float column
df.a = df.a.astype('pint[g]')

# dtype assignment to ufloat column causes the issue
df.b = df.b.astype('pint[g]')
# uarray creation before dtype assignment leads to same issue
n = [x.n for x in df.b]
s = [x.s for x in df.b]
ua = unumpy.uarray(n, s)

df.b = pd.Series(ua, dtype='pint[g]')
# and the usage of the PintArray constructor directly as well (as expected)
df.b = pint_pandas.PintArray(ua, 'g')

A compatibility between PintArray and uncertainties, as fulfilled by the Measurement class in pint would in my opinion be a huge improvement in pint-pandas.

@Cs137 Cs137 changed the title Incompatibility with 'uncertainties'? Compatibility with 'uncertainties'? Apr 30, 2022
@andrewgsavage
Copy link
Collaborator

Yes, this would be nice.

@hgrecco has been wanting to separate out Measurement from pint, which might mean you want a separate package for uncertainties with pandas. Probably worth waiting till that's done too - although that's been on the agenda for a while now.

@MichaelTiemannOSC
Copy link
Collaborator

xref: #140

@andrewgsavage
Copy link
Collaborator

closing this as it's now possible with UncertaintyArray
https://pint-pandas.readthedocs.io/en/latest/user/initializing.html

@Cs137
Copy link
Author

Cs137 commented Jan 20, 2025

Since I still miss the possibility to write a DataFrame with "pint uncertainty series" into a file and restore it later on, I wrote Series/DataFrame accessors that allow to (de)convolute such series. The accessors can be used to restore a DataFrame with uncertainties and units as described here.

@andrewgsavage
Copy link
Collaborator

Since I still miss the possibility to write a DataFrame with "pint uncertainty series" into a file and restore it later on, I wrote Series/DataFrame accessors that allow to (de)convolute such series. The accessors can be used to restore a DataFrame with uncertainties and units as described here.

some (maybe all?) of those accessors could be added to uncertainties-pandas / pint-pandas

would it be worth doing something to return Measurement objects?

@Cs137
Copy link
Author

Cs137 commented Jan 20, 2025

some (maybe all?) of those accessors could be added to uncertainties-pandas / pint-pandas

I thought about this as well, but since it requires both, pint-pandas and uncertainty-pandas, it does not really fit into one of them. But I'd be happy to hear your thoughts, @andrewgsavage, in case you disagree.

Cheers,
Christian

@andrewgsavage
Copy link
Collaborator

There should be accessors for .n, .s for uncertainties-pandas that operate on floats, without units.

I am less sure on whether the pint+uncertainties accessors should be in pint-pandas or elsewhere. I think hgrecco mentioned splitting measurements outside of pint but that was a while ago. What you have is fine for now, we can see how well this works for people - which will need adding links to docs so they can find it!

@Cs137
Copy link
Author

Cs137 commented Jan 20, 2025

Thanks, I will certainly add links to the corresponding documentations of pint-pandas and uncertainties-pandas and need to add some descriptions to clarify my wording and harmonise the latter. The focus of the last weekend was the creation of a working codebase and corresponding tests. I hope to find some time during the week to pick the mentioned items up.

Moreover, I agree on your statement regarding the need for uncertainties-pandas accessors that allow to retrieve the n and/or s series. If there is even more time during the week than anticipated, I will try to draft something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants