{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/5868","labels_url":"https://api.github.com/repos/numpy/numpy/issues/5868/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/5868/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/5868/events","html_url":"https://github.com/numpy/numpy/issues/5868","id":75342763,"number":5868,"title":"vectorize fails for zero-dimensioned arrays","user":{"login":"jordigh","id":260143,"avatar_url":"https://avatars.githubusercontent.com/u/260143?v=3","gravatar_id":"","url":"https://api.github.com/users/jordigh","html_url":"https://github.com/jordigh","followers_url":"https://api.github.com/users/jordigh/followers","following_url":"https://api.github.com/users/jordigh/following{/other_user}","gists_url":"https://api.github.com/users/jordigh/gists{/gist_id}","starred_url":"https://api.github.com/users/jordigh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jordigh/subscriptions","organizations_url":"https://api.github.com/users/jordigh/orgs","repos_url":"https://api.github.com/users/jordigh/repos","events_url":"https://api.github.com/users/jordigh/events{/privacy}","received_events_url":"https://api.github.com/users/jordigh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-05-11T20:13:26Z","updated_at":"2015-05-14T20:47:19Z","closed_at":null,"body":"The following used to work and return an empty 5 by 0 array.\r\n\r\n```python\r\nnp.vectorize(lambda x: x+1)(np.zeros([5,0]))\r\n```\r\n\r\nNow it stack traces:\r\n\r\n```\r\n/usr/lib/python2.7/dist-packages/numpy/lib/function_base.pyc in __call__(self, *args, **kwargs)\r\n   1571             vargs.extend([kwargs[_n] for _n in names])\r\n   1572 \r\n-> 1573         return self._vectorize_call(func=func, args=vargs)\r\n   1574 \r\n   1575     def _get_ufunc_and_otypes(self, func, args):\r\n\r\n/usr/lib/python2.7/dist-packages/numpy/lib/function_base.pyc in _vectorize_call(self, func, args)\r\n   1631             _res = func()\r\n   1632         else:\r\n-> 1633             ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)\r\n   1634 \r\n   1635             # Convert args to object arrays first\r\n\r\n/usr/lib/python2.7/dist-packages/numpy/lib/function_base.pyc in _get_ufunc_and_otypes(self, func, args)\r\n   1594             # Assumes that ufunc first evaluates the 0th elements in the input\r\n   1595             # arrays (the input values are not checked to ensure this)\r\n-> 1596             inputs = [asarray(_a).flat[0] for _a in args]\r\n   1597             outputs = func(*inputs)\r\n   1598 \r\n\r\nIndexError: index 0 is out of bounds for axis 0 with size 0\r\n> /usr/lib/python2.7/dist-packages/numpy/lib/function_base.py(1596)_get_ufunc_and_otypes()\r\n   1595             # arrays (the input values are not checked to ensure this)\r\n-> 1596             inputs = [asarray(_a).flat[0] for _a in args]\r\n   1597             outputs = func(*inputs)\r\n```\r\n\r\nSeems to me that there should first be a check for empty arrays before attempting to see the return types of the function?"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/102164584","html_url":"https://github.com/numpy/numpy/issues/5868#issuecomment-102164584","issue_url":"https://api.github.com/repos/numpy/numpy/issues/5868","id":102164584,"user":{"login":"jordigh","id":260143,"avatar_url":"https://avatars.githubusercontent.com/u/260143?v=3","gravatar_id":"","url":"https://api.github.com/users/jordigh","html_url":"https://github.com/jordigh","followers_url":"https://api.github.com/users/jordigh/followers","following_url":"https://api.github.com/users/jordigh/following{/other_user}","gists_url":"https://api.github.com/users/jordigh/gists{/gist_id}","starred_url":"https://api.github.com/users/jordigh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jordigh/subscriptions","organizations_url":"https://api.github.com/users/jordigh/orgs","repos_url":"https://api.github.com/users/jordigh/repos","events_url":"https://api.github.com/users/jordigh/events{/privacy}","received_events_url":"https://api.github.com/users/jordigh/received_events","type":"User","site_admin":false},"created_at":"2015-05-14T20:47:19Z","updated_at":"2015-05-14T20:47:19Z","body":"> Actually, it seems to me it didn't really worked in 1.6, even if it did not raise an error\r\n\r\nI can't easily check on numpy 1.6 now because it's gone with my OS upgrade. But whatever it did was better than stack tracing. I am guessing the type might have been irrelevant because it was for an empty array, so there were no elements whose type could have clashed with whatever other thing I was doing.\r\n\r\n> or am I reading it wrong?\r\n\r\nI think so. I did not mean np.int, but rather np.int(). Note the function call.\r\n\r\n> if you need it to work on empty arrays, the return type should be explicitly specified, as @seberg has demonstrated.\r\n\r\nThis is not the best way to handle empty arrays, because I don't know a priori if I need it to work on empty arrays or not. I am guessing you are familiar with [the following](ftp.cs.wisc.edu/Approx/empty.pdf)? The Matlab folks long ago figured that handing empty arrays transparently is a very useful and natural thing to do. This is especially important in light of boolean indexing, which can produce empty arrays at any time.\r\n\r\nThe special-casing you suggest would effectively mean that np.vectorize would always have to be called with the type of the output argument, which seems like an undue burden upon the caller. An alternative could be to assume that the output type is NoneType and only change this default if there is anything to change it to."}}