Repository navigation
ENH: adds np.nancumsum and np.nancumprod - #7421
Conversation
|
cc @shoyer and pydata/xarray#791 |
Corresponds to PR numpy#7421, adpated from numpy#7410
|
Just a note -- since this is new API, somebody might ask you to write the numpy-discussions mailing list. But I think this should be pretty uncontroversial 👍. |
|
Consistency checks with integer arrays are great, but these also need tests on arrays with NaNs. |
|
Thanks @shoyer, I accidentally left that out. I updated this to include tests on arrays with NaNs following |
|
Could you put the documentation commit in this PR as well? Note that you can keep adding commits to the PR, and later clean up the history with |
There was a problem hiding this comment.
We're already at the release candidate stage for 1.11, so this will make it into 1.12
There was a problem hiding this comment.
I guess this is supposed to say zero?
There was a problem hiding this comment.
@seberg, thanks for finding that typo (should have been plural form of verb too, not singular-- same also for nancumprod)
There was a problem hiding this comment.
While you're at it, nans are treated like "one", not "zero" on line 542/545.
There was a problem hiding this comment.
Also, if I am not mistaken, axis, two lines below, accepts a tuple now. The docs should be updated according to np.prod.
There was a problem hiding this comment.
Not for the .accumulate() method used by the cumxxx functions: only one axis.
There was a problem hiding this comment.
My mistake. By the way, would it make sense to apply the _ureduce function from numpy.lib.function_base to other places, like most of numpy.core.from_numeric? That way we could add multi-axis support without waiting for the gufunc redesign that seems to be coming.
There was a problem hiding this comment.
Which functions do you have in mind? I don't see any obvious place where _ureduce could help...
There was a problem hiding this comment.
ptp, argmin, argmax. Also, the following might benefit as well, but the internal order of the array would matter: partition, argpartition, sort, argsort, searchsorted, cumsum, cumprod. I was even thinking that it might be worth allowing ravel to do a partial ravel along a subset of the axes (basically like _ureduce does), but that is probably going to require some major rewriting to do properly.
There was a problem hiding this comment.
Thanks @madphysicist, change made by 8fd1ee8 fixes the typo.
There was a problem hiding this comment.
Only that I made a mistake. Axis only accepts one value here. You were absolutely right.
There was a problem hiding this comment.
Mention that the sum does not change when nans are encountered and that any leading nans are replaced by zeros.
There was a problem hiding this comment.
@madphysicist, thanks, should be fixed by 41adc0f
There was a problem hiding this comment.
Have you pushed that commit yet?
|
Aside from the comments about docs, LGTM. And by all means feel free to ignore the comments about functions other than your own. |
|
@madphysicist, I've pushed commits to address your comments (including for |
|
@madphysicist, actually there are more following a page refresh... hold on a bit. I'll let you know when the others are pushed. |
There was a problem hiding this comment.
Sorry to nitpick, but the line lengths look funny here. Also, _along_axis is inconsistent with the other functions. I think you should just leave it as nansum.
There was a problem hiding this comment.
Agreed I wondered about that myself but didn't want to depart from convention. Fixed in 29cca2d
|
Reading PEP8 more carefully, it looks like I'm wrong about spacing around binary operators: Hmm.... |
|
@shoyer, reverted spacing back on |
There was a problem hiding this comment.
I think this should probably be all one big array -- it's only not one array for _rdat because _rdat is ragged
|
@pwolfram I think this is very close, though a test for negative axes would be nice. Otherwise this looks good to me. Sorry for leading your astray on pep8! |
There was a problem hiding this comment.
@shoyer, was this what you were thinking?
This PR adds an implementation of `nancumsum` and `nancumprod`. The actual function is a two-liner adapted from `nansum`. Its structure is adapted from PR: numpy#5418
| res = nf(mat, axis=axis, out=resout) | ||
| assert_almost_equal(res, resout) | ||
| assert_almost_equal(res, tgt) | ||
|
|
There was a problem hiding this comment.
@shoyer, I also generalized the test here for consistency and greater coverage.
|
@shoyer, please see above tests added over the negative axis. Thanks for helping refine this! |
|
Also, btw @shoyer, no worries about the pep8 confusion. I installed the plugin for vim and I have a clearer idea of formatting standards so it was certainly value-added! |
|
OK, this looks good to me. I'll merge this once tests pass unless anyone speaks up with objections... |
|
Thanks @shoyer! |
Needed until numpy v1.12, see numpy/numpy#7421
Needed until numpy v1.12, see numpy/numpy#7421
Needed until numpy v1.12, see numpy/numpy#7421
Needed until numpy v1.12, see numpy/numpy#7421
Needed until numpy v1.12, see numpy/numpy#7421
Needed until numpy v1.12, see numpy/numpy#7421
Needed until numpy v1.12, see numpy/numpy#7421
* Adds nancumsum, nancumprod for numpy compatability Needed until numpy v1.12, see numpy/numpy#7421 * Adds nancumsum, nancumprod to xarray functions
This PR adds an implementation of
nancumsumandnancumprod.The actual function is a two-liner adapted from
nansumandnanprod.Its structure is adapted from PR: #5418 ( a minor typo in the doc string from this PR is fixed too)