-
Notifications
You must be signed in to change notification settings - Fork 472
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
NEP-18 and python scalars #950
Comments
Would you be able to explain further what is problematic and/or wrong about the current behavior? For instance, when a Quantity wraps a Python scalar, there is the following behavior: >>> from pint import Quantity
>>> x = Quantity(2, 'meter')
>>> x
<Quantity(2, 'meter')>
>>> x.shape
Traceback (most recent call last):
File ".../quantity.py", line 1575, in __getattr__
return getattr(self._magnitude, item)
AttributeError: 'int' object has no attribute 'shape'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../quantity.py", line 1579, in __getattr__
"has attribute '{}'".format(self._magnitude, item)
AttributeError: Neither Quantity object nor its magnitude (2) has attribute 'shape'
>>> x.ndim
Traceback (most recent call last):
File ".../quantity.py", line 1575, in __getattr__
return getattr(self._magnitude, item)
AttributeError: 'int' object has no attribute 'ndim'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../quantity.py", line 1579, in __getattr__
"has attribute '{}'".format(self._magnitude, item)
AttributeError: Neither Quantity object nor its magnitude (2) has attribute 'ndim' And when a NumPy scalar is wrapped: >>> y = Quantity(np.arange(3), 'meter')[-1]
>>> y
<Quantity(2, 'meter')>
>>> y.shape
()
>>> y.ndim
0 Pint Quantity scalars also need to respond to >>> np.arange(2) * Quantity(2, 'meter')
<Quantity([0 2], 'meter')> At least to me, this all seems like reasonable, expected behavior for a flexible wrapper class. Also, on a related topic, there has previously been discussion of splitting the Quantity class into a scalar and a sequence class, but that now seems to be disfavored (see #753). |
I've been trying to use xarray's So the question is: what can we reasonably expect from an object that implements |
I think these are two different questions. While based on xarray's implementation, I had initially assumed that an object having So, I would consider a Pint Quantity to always support However, this is all just my interpretation of the relevant documentation, and I very well may be mistaken. If you get the chance, @crusaderky and @shoyer, would you mind weighing in? |
Sorry about that, I didn't read the NEPs carefully enough which lead me to consider implementing Would it be reasonable for |
I'm honestly not sure!
If all these attributes are needed in the way NumPy expects them, perhaps it would just be better to have the scalars converted in the first place? It would likely need to be opt-in, so something like the current |
👍 for adding thanks for the trick with |
I'd say go ahead if you want. I can add notes on |
We now implement
__array_function__
which makespint.Quantity
seem like anumpy
duck array. However, if we usepint
to wrap python scalars, this is wrong: python scalars don't have attributes likeshape
orndim
.I don't know how we could fix this but it is something we definitely should not leave as it is right now.
The text was updated successfully, but these errors were encountered: