{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/5706","labels_url":"https://api.github.com/repos/numpy/numpy/issues/5706/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/5706/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/5706/events","html_url":"https://github.com/numpy/numpy/pull/5706","id":63484998,"number":5706,"title":"ENH: make some masked array methods behave more like ndarray methods","user":{"login":"ahaldane","id":9040124,"avatar_url":"https://avatars.githubusercontent.com/u/9040124?v=3","gravatar_id":"","url":"https://api.github.com/users/ahaldane","html_url":"https://github.com/ahaldane","followers_url":"https://api.github.com/users/ahaldane/followers","following_url":"https://api.github.com/users/ahaldane/following{/other_user}","gists_url":"https://api.github.com/users/ahaldane/gists{/gist_id}","starred_url":"https://api.github.com/users/ahaldane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahaldane/subscriptions","organizations_url":"https://api.github.com/users/ahaldane/orgs","repos_url":"https://api.github.com/users/ahaldane/repos","events_url":"https://api.github.com/users/ahaldane/events{/privacy}","received_events_url":"https://api.github.com/users/ahaldane/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/numpy/numpy/labels/component:%20numpy.ma","name":"component: numpy.ma","color":"ff0000"},{"url":"https://api.github.com/repos/numpy/numpy/labels/Enhancement","name":"Enhancement","color":"e102d8"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-03-22T03:41:57Z","updated_at":"2015-03-28T21:32:08Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/numpy/numpy/pulls/5706","html_url":"https://github.com/numpy/numpy/pull/5706","diff_url":"https://github.com/numpy/numpy/pull/5706.diff","patch_url":"https://github.com/numpy/numpy/pull/5706.patch"},"body":"\r\nI recently learned about masked arrays and was trying them out in a project, and found that the masked array functions `mean`, `var`, `sum`, `amax` and similar do not behave quite the same as their ndarray counterparts. \r\n\r\nThey don't support multiple axes like ndarrays, eg\r\n\r\n    >>> a = np.random.rand(4,4)\r\n    >>> a.mean(axis=(0,1)) # works\r\n    >>> b = np.ma.array(a, mask=np.random.rand(4,4)<0.5)\r\n    >>> b.mean(axis=(0,1)) # fails with IndexError\r\n\r\nthey don't have a `keepdims` argument, and looking at the code the behavior is a little different.\r\n\r\nIn this PR I modified `np.ma.mean`, `np.ma.sum` and `np.ma.count` to behave more like ndarray methods, as proof of concept. \r\n\r\nDoes it look like a good idea, and is the code OK so far? If so I'll do the same thing for the rest of the functions (`var`, `sum` etc).\r\n\r\nNote that I modified a test to get it to pass: I removed the test that the return type of `np.ma.count` was of type `np.intp`. The return type can now be an ndarray (for partial axes) or an int. The return type was discussed in https://github.com/numpy/numpy/issues/4698, and I think the problem at that time was that `arr.size` could return variable types, but I removed dependence on `arr.size` so I don't think that's a problem any more."},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/87316235","html_url":"https://github.com/numpy/numpy/pull/5706#issuecomment-87316235","issue_url":"https://api.github.com/repos/numpy/numpy/issues/5706","id":87316235,"user":{"login":"ahaldane","id":9040124,"avatar_url":"https://avatars.githubusercontent.com/u/9040124?v=3","gravatar_id":"","url":"https://api.github.com/users/ahaldane","html_url":"https://github.com/ahaldane","followers_url":"https://api.github.com/users/ahaldane/followers","following_url":"https://api.github.com/users/ahaldane/following{/other_user}","gists_url":"https://api.github.com/users/ahaldane/gists{/gist_id}","starred_url":"https://api.github.com/users/ahaldane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ahaldane/subscriptions","organizations_url":"https://api.github.com/users/ahaldane/orgs","repos_url":"https://api.github.com/users/ahaldane/repos","events_url":"https://api.github.com/users/ahaldane/events{/privacy}","received_events_url":"https://api.github.com/users/ahaldane/received_events","type":"User","site_admin":false},"created_at":"2015-03-28T21:32:08Z","updated_at":"2015-03-28T21:32:08Z","body":"Updated. Summary so far:\r\n\r\n * reworked count method (to allow multiple axes)\r\n * added keepdims arg to all, any, sum, prod, mean, var, std, min, max\r\n * added (unused) arg to same methods in matrix class\r\n * updated docstring for those funcs, plus cumsum, cumprod, std, round.\r\n * rewrote internals of mean and var. (var was pretty buggy!)\r\n * removed anom method, which was just used in var before\r\n * hid warnings during domain calculations\r\n\r\nI haven't tested it much yet.\r\n\r\nBy the way, for a separate PR, I noticed the ndarray doctrings are missing keepdim args. Eg in `ndarray.mean` and others.\r\n\r\nI also notices that the C-Api function `PyArray_Mean` is implemented (in `calculations.c`) quite differently from `ndarray.mean` (in `core._methods.py`). (and var and others too) Probably it should simply call array_mean.\r\n\r\n"}}