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

Support constructing a Quantity from a NDData object #16322

Open
Cadair opened this issue Apr 22, 2024 · 2 comments
Open

Support constructing a Quantity from a NDData object #16322

Cadair opened this issue Apr 22, 2024 · 2 comments

Comments

@Cadair
Copy link
Member

Cadair commented Apr 22, 2024

What is the problem this feature will solve?

In [3]: c = NDData(np.zeros((10,10)), unit=u.count)

In [4]: u.Quantity(c)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 u.Quantity(c)

File ~/.virtualenvs/sunpy-dev/lib/python3.11/site-packages/astropy/units/quantity.py:552, in Quantity.__new__(cls, value, unit, dtype, copy, order, subok, ndmin)
    548 # check that array contains numbers or long int objects
    549 if value.dtype.kind in "OSU" and not (
    550     value.dtype.kind == "O" and isinstance(value.item(0), numbers.Number)
    551 ):
--> 552     raise TypeError("The value must be a valid Python or Numpy numeric type.")
    554 # by default, cast any integer, boolean, etc., to float
    555 if float_default and value.dtype.kind in "iuO":

TypeError: The value must be a valid Python or Numpy numeric type.

Describe the desired outcome

a NDData object has a .unit and is "array like" I feel like this should work, and be equivalent to u.Quantity(c.data, unit=c.unit).

Additional context

No response

@neutrinoceros
Copy link
Contributor

This feels like something that could be addressed in the Quantity 2.0 project. What do you think @nstarman ?

@nstarman
Copy link
Member

nstarman commented Apr 22, 2024

I'm hoping that in Q2.0 we simplify the construction of Quantity, further optimizing it for speed. Like NumPy, Jax, etc have asarray we would then provide a quantity() function that munges all the various objects into a Quantity. So yes, this should be supported!

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

No branches or pull requests

3 participants