hgrecco / pint

Operate and manipulate physical quantities in Python
http://pint.readthedocs.org/
Other
2.38k stars 465 forks source link

Using Quantity in np.linspace with option retstep=True raises VisibleDeprecationWarning #1340

Open mmarras opened 3 years ago

mmarras commented 3 years ago

pint: 0.17 numpy: 1.21.0 python: 3.9.1


Hi,

the docs state that np.linspace is supported by pint, thus maybe this would be nice to have someone looking into.

time = 10 * ureg.s
np.linspace(0, time, num = 50)

is fine, but

np.linspace(0, time, num = 50, retstep=True)

raises

~\site-packages\numpy\core_asarray.py:102: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray. return array(a, dtype, copy=False, order=order)

According to the inet, has to do with a change introduced in numpy 1.19 requiring explicit statment of 'dtype=object' when creating those arrays.

I guess this is a workaround, so is not too critical.

ureg = pint.UnitRegistry()
time = 10 * ureg.s
numpy.linspace(0, time, num=50)
numpy.linspace(0, time.magnitude, num=50, retstep=True)[1] * time.units

But I guess this may not be the only numpy function which does something like this.

hgrecco commented 3 years ago

Indeed, this was working with numpy 1.18.5 but not with +1.19.

Would you like to submit a PR, take a look at the current generic implementation here