# [Numpy-discussion] is __array_ufunc__ ready for prime-time?

Marten van Kerkwijk m.h.vankerkwijk at gmail.com
Thu Nov 2 11:51:54 EDT 2017

```Hi Josef,

astropy's Quantity is well developed and would give similar results to
pint; all those results  make sense if one wants to have consistent
units. A general library code will actually do the right thing as long
as it just uses normal mathematical operations with ufuncs - and as
long as it just duck types! - the unit code will then override and
properly propagate units to outputs, as can be seen in this example:
```
import astropy.units as u
np.fft.fftfreq(8, 1*u.min)
# <Quantity [ 0.   , 0.125, 0.25 , 0.375,-0.5  ,-0.375,-0.25 ,-0.125] 1 / min>
np.fft.fftfreq(8, 1*u.min).var()
# <Quantity 0.08203125 1 / min2>
```

> for example if units are some flows per unit of time and we average, sum or integrate over time, then what are the new units? (e.g. pandas time aggregation)

The units module will force you to take into account `dt`! This is in
fact one reason why it is so powerful. So, your example might go
something like:
```
flow = [1., 1.5, 1.5] * u.g / u.s
dt = [0.5, 0.5, 1.] * u.hr
np.sum(flow * dt)
# <Quantity 2.75 g h / s>
np.sum(flow * dt).to(u.kg)
# <Quantity 9.9 kg>
```

> How do you concatenate and operate arrays with different units?

This is where Nathaniel's `__array_concatenate__` would come in. For
regular arrays it is fine to just concatenate, but for almost anything
else you need a different approach. For quantities, the most logical
one would be to first create an empty array of the right size with the
unit of, e.g., the first part to be concatenated, and then set
sections to the input quantities (where the setter does unit
conversion and will fail if that is not possible).

All the best,

Marten

p.s. A fun subject is what to do with logarithmic units, such as the
magnitudes in astronomy... We have a module for that as well;
http://docs.astropy.org/en/latest/units/logarithmic_units.html
```