{"action":"opened","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/5591","labels_url":"https://api.github.com/repos/numpy/numpy/issues/5591/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/5591/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/5591/events","html_url":"https://github.com/numpy/numpy/issues/5591","id":58465693,"number":5591,"title":"BUG: numpy.ma.polyfit masks NaNs incorrectly","user":{"login":"simongibbons","id":950457,"avatar_url":"https://avatars.githubusercontent.com/u/950457?v=3","gravatar_id":"","url":"https://api.github.com/users/simongibbons","html_url":"https://github.com/simongibbons","followers_url":"https://api.github.com/users/simongibbons/followers","following_url":"https://api.github.com/users/simongibbons/following{/other_user}","gists_url":"https://api.github.com/users/simongibbons/gists{/gist_id}","starred_url":"https://api.github.com/users/simongibbons/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simongibbons/subscriptions","organizations_url":"https://api.github.com/users/simongibbons/orgs","repos_url":"https://api.github.com/users/simongibbons/repos","events_url":"https://api.github.com/users/simongibbons/events{/privacy}","received_events_url":"https://api.github.com/users/simongibbons/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-02-21T16:51:27Z","updated_at":"2015-02-21T16:51:27Z","closed_at":null,"body":"The current implementation of ``numpy.ma.polyfit`` fails with masked arrays that mask NaNs.\r\n\r\nFor example this :\r\n\r\n    import numpy as np\r\n    x = np.array([1, 2, np.nan, 4])\r\n    y = np.array([1, 2, 3, 4])\r\n\r\n    x_masked = np.ma.masked_invalid(x)\r\n    y_masked = np.ma.masked_invalid(y)\r\n\r\n    print np.ma.polyfit(x_masked, y_masked, 1)\r\n\r\nyields the following error message when run using numpy 1.9.0 and the latest development version with python 2.7.9 on mac OSX 10.10\r\n\r\n    ** On entry to DLASCL, parameter number  4 had an illegal value\r\n    ** On entry to DLASCL, parameter number  4 had an illegal value\r\n    ** On entry to DLASCL, parameter number  4 had an illegal value\r\n    ** On entry to DLASCL, parameter number  4 had an illegal value\r\n    ** On entry to DLASCL, parameter number  5 had an illegal value\r\n    ** On entry to DLASCL, parameter number  4 had an illegal value\r\n    [ nan  nan]\r\n\r\nI think this is due to the way ``numpy.ma.polyfit`` is implemented. Currently it sets the weight of any point that is masked in the fit to zero which is fine for any finite value of the input arrays, however if one of the masked values is NaN then it will still propagate through to the result.\r\n\r\nWhilst an easy fix would be to pass only a slice of the unmasked values through to ``np.polyfit`` I'm loathe to suggest that as the ideal solution as it would be much more inefficient for any case without masked NaNs."}}