[Numpy-discussion] NEP 42 and physical unit DType

Sebastian Berg sebastian at sipsolutions.net
Fri Oct 9 13:08:07 EDT 2020


On Fri, 2020-10-09 at 07:48 -0500, Lee Johnston wrote:
> NEP 42 mentions physical units as a possible use case for the new
> DType.
> Having worked on `unyt`, which is an ndarray subclass, and other unit
> system implementations based on the dispatch mechanism, I am quite
> familiar
> with the challenges. One challenge is integration with Matplotlib
> that has
> a unit conversion interface. This interface is invoked by a registry
> mapping from unit array type, such as `unyt_array`, and its
> conversion
> class. A custom DType will still result in an ndarray type - correct?

A custom DType will be attached to a normal NumPy array (or, hopefully
a pandas dataframe, etc.). I am not sure if that is what you meant, but
a custom DType will mean there is no array subclass involved (this is
different from how pandas approaches extension dtypes).
But, I am not sure that this makes much of a difference with respect to
the problem.

> If
> so, Matplotlib will attempt to perform its internal calculations
> eventually
> leading to an invalid operation when adding a physical quantity to a
> pure
> number. This happens today with `unyt_array` and some of the plotting

It sounds unlikely that this would solve the issue immediately. Rather,
we probably need to think about creating some kind of "protocol" to
drop units, such as:

   arr.astype(Unitless)

which would be a no-op for the NumPy numerical types. Or baking in
something similar to make working with units easier.

I would have hoped a bit that it is possible to avoid such issues e.g.
by dividing by a step/difference, but I do not know the details.

The most important question for me would be more whether there is
something in the basic infrastructure to consider that cannot be done
as an extension/protocol later (like the `arr.astype(Unitless)` cast,
which should be a straight forward extension).

Although, it would be interesting to figure out how a future Unit DType
would best look in this regard!

Cheers,

Sebastian


> functions that allow the subclass to leak through into the internals.
> Has
> this been discussed? I can imagine other libraries will have the same
> challenge.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20201009/6bc75e4c/attachment.sig>


More information about the NumPy-Discussion mailing list