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 for numpy NaN functions: nan{min,max,mean,argmin,argmax,sum} #126

Closed
mankoff opened this issue Apr 4, 2014 · 7 comments · Fixed by #905
Closed

Support for numpy NaN functions: nan{min,max,mean,argmin,argmax,sum} #126

mankoff opened this issue Apr 4, 2014 · 7 comments · Fixed by #905
Labels
numpy Numpy related bug/enhancement

Comments

@mankoff
Copy link
Contributor

mankoff commented Apr 4, 2014

No description provided.

@hgrecco
Copy link
Owner

hgrecco commented Apr 4, 2014

It works fine in my computer (Pint develop branch)

>>> import numpy as np
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> [1., 2., 3., float('NaN')] * ureg.meter
<Quantity([  1.   2.   3.  nan], 'meter')>
>>> q = [1., 2., 3., float('NaN')] * ureg.meter
>>> np.nanmean(q)
2.0
>>> np.nanmax(q)
3.0
>>> np.nanargmin(q)
0
>>> np.nanargmax(q)
2

What it does not work (and should) is assigning a NaN:

>>> q[0] = np.nan
Traceback (most recent call last):
...
ValueError
>>> q[0] = float('NaN')
Traceback (most recent call last):
...
ValueError

I am opening a new issue for this.

@hgrecco
Copy link
Owner

hgrecco commented Apr 4, 2014

Sorry. My mistake. nanmean and nanmax does not work as the units are lost. I guess this is related to the fact that numpy does not recognize classes not derived from ndarray as numpy arrays.

@mankoff
Copy link
Contributor Author

mankoff commented Apr 4, 2014

np.nanmean(q) works, but units a dropped. q.mean() works and retains units. q.nanmean() does not work. The latter is what I would like, so that units are retained. Note that when uncertainty propagation is fully implemented, it will (should?) work with q.mean(), but I'm not sure how it would work with np.mean(q). Similarly, uncertainties should also work with q.nanmean().

Also, q[0] = np.nan * ureg.m does work.

@hgrecco
Copy link
Owner

hgrecco commented Apr 4, 2014

(Oops, it seems that we commented together) Also, please take a look at the docs and this numpy issue. NumPy devs don't think that this is a problem as the issue was closes right away. I am seriously considering having an additional _NPQuantity class for 0.6 to fix this issue and also, #39 and #37

@mankoff mankoff changed the title Support for more numpy operators: nanmean, nanmax, nanargmin, nanargmax Support for numpy NaN functions: nan{min,max,mean,argmin,argmax,sum} Apr 4, 2014
@mankoff
Copy link
Contributor Author

mankoff commented Apr 5, 2014

I'm fairly new to this level of Python development, but it seems we cannot / should not patch numpy so that np.mean(q) returns units based on this Q&A: https://stackoverflow.com/questions/22600365/can-i-decorate-an-explicit-function-call-such-as-np-sqrt However, we can write pint so that q.mean() returns units.

@hgrecco
Copy link
Owner

hgrecco commented Apr 5, 2014

Pint supports NumPy functions without monkey patching NumPy. It already supports mean since 0.2 (see https://github.com/hgrecco/pint/blob/master/pint/testsuite/test_numpy.py#L146)
The problem with nanmean, etc is explained above.

@hgrecco
Copy link
Owner

hgrecco commented Dec 3, 2019

Review once that #905 is merged

@hgrecco hgrecco added the numpy Numpy related bug/enhancement label Dec 3, 2019
@bors bors bot closed this as completed in 43fbae2 Dec 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
numpy Numpy related bug/enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants