{"action":"created","issue":{"url":"https://api.github.com/repos/numpy/numpy/issues/6430","labels_url":"https://api.github.com/repos/numpy/numpy/issues/6430/labels{/name}","comments_url":"https://api.github.com/repos/numpy/numpy/issues/6430/comments","events_url":"https://api.github.com/repos/numpy/numpy/issues/6430/events","html_url":"https://github.com/numpy/numpy/pull/6430","id":110551051,"number":6430,"title":"Fix issues with zero-width string fields","user":{"login":"embray","id":676149,"avatar_url":"https://avatars.githubusercontent.com/u/676149?v=3","gravatar_id":"","url":"https://api.github.com/users/embray","html_url":"https://github.com/embray","followers_url":"https://api.github.com/users/embray/followers","following_url":"https://api.github.com/users/embray/following{/other_user}","gists_url":"https://api.github.com/users/embray/gists{/gist_id}","starred_url":"https://api.github.com/users/embray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/embray/subscriptions","organizations_url":"https://api.github.com/users/embray/orgs","repos_url":"https://api.github.com/users/embray/repos","events_url":"https://api.github.com/users/embray/events{/privacy}","received_events_url":"https://api.github.com/users/embray/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/numpy/numpy/labels/component:%20numpy.core","name":"component: numpy.core","color":"ff0000"},{"url":"https://api.github.com/repos/numpy/numpy/labels/Maintenance","name":"Maintenance","color":"02e10c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":24,"created_at":"2015-10-08T22:05:59Z","updated_at":"2015-10-22T03:17:09Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/numpy/numpy/pulls/6430","html_url":"https://github.com/numpy/numpy/pull/6430","diff_url":"https://github.com/numpy/numpy/pull/6430.diff","patch_url":"https://github.com/numpy/numpy/pull/6430.patch"},"body":"There is a behavior that probably dates far back, that Numpy tries really hard not to let you make arrays with zero-width strings as the dtype.  It normally will promote them to a width of at least one (or more, depending on the context).  That can be a little surprising, but we probably don't want to change it.\r\n\r\nHowever, (perhaps as an oversight) it is possible to make a structured array that has zero-width strings for one of its fields.  But when viewing such a field the dtype of the view is converted to 'S1' (or 'U1' for unicode), leading to data corruption, overflows, etc.  So this PR ensures that arrays of zero-width strings *can* be created at least in the special case of viewing a field of a structured array.\r\n\r\nThis also lifts the restrictions in the `dtype` constructor that prevented creation of structured dtypes with zero-width fields.\r\n\r\nFixes #2196, #473, #4955, #2585, and #1901 .\r\n\r\n(I'll add some regression tests in a bit)"},"comment":{"url":"https://api.github.com/repos/numpy/numpy/issues/comments/150095183","html_url":"https://github.com/numpy/numpy/pull/6430#issuecomment-150095183","issue_url":"https://api.github.com/repos/numpy/numpy/issues/6430","id":150095183,"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-10-22T03:17:09Z","updated_at":"2015-10-22T03:17:09Z","body":"The code looks good to me. I would just like to try to think briefly of any special cases we might have missed.\r\n\r\nFor instance, saving the zero-sized arrays with `np.save` doesn't work with the error from above. I'm ok leaving that broken for now though (who would want to do it?).\r\n\r\nHere are a few more cases I thought about:\r\n\r\n    >>> a = np.zeros(3, dtype='S0,u1')['f0']  # easiest way to get a 0-sized array?\r\n    >>> a.view('S0')\r\n    TypeError: data-type must not be 0-sized\r\n\r\nThat seems fine to leave alone.\r\n\r\n    >>> np.empty(3, dtype='S0,S0')\r\n    TypeError: Empty data-type\r\n\r\nsame.\r\n\r\n    >>> a = np.zeros(4, dtype=[('a', 'S0,S0'), ('b', 'u1')])\r\n    >>> a['a']\r\n    TypeError: Empty data-type\r\n\r\nThat's more dubious.\r\n\r\n    >>> a = np.zeros(4, dtype='S0,u1')['f0']\r\n    >>> a.reshape((2,2))\r\n    array([['', ''],\r\n           ['', '']], dtype='|S1')\r\n\r\nThis also seems a bit dubious.\r\n\r\nAny thoughts on those?"}}