{"action":"created","issue":{"url":"https://api.github.com/repos/pandas-dev/pandas/issues/20843","repository_url":"https://api.github.com/repos/pandas-dev/pandas","labels_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20843/labels{/name}","comments_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20843/comments","events_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20843/events","html_url":"https://github.com/pandas-dev/pandas/issues/20843","id":318431485,"number":20843,"title":"Index.astype('category') does not work","user":{"login":"h-vetinari","id":33685575,"avatar_url":"https://avatars1.githubusercontent.com/u/33685575?v=4","gravatar_id":"","url":"https://api.github.com/users/h-vetinari","html_url":"https://github.com/h-vetinari","followers_url":"https://api.github.com/users/h-vetinari/followers","following_url":"https://api.github.com/users/h-vetinari/following{/other_user}","gists_url":"https://api.github.com/users/h-vetinari/gists{/gist_id}","starred_url":"https://api.github.com/users/h-vetinari/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/h-vetinari/subscriptions","organizations_url":"https://api.github.com/users/h-vetinari/orgs","repos_url":"https://api.github.com/users/h-vetinari/repos","events_url":"https://api.github.com/users/h-vetinari/events{/privacy}","received_events_url":"https://api.github.com/users/h-vetinari/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-04-27T14:30:21Z","updated_at":"2018-04-27T18:57:45Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"It's easy to make a Series categorical by using `.astype('category')`, and this yields the same result as calling the constructor with `dtype='category'`.\r\n\r\nThe same call fails for Index:\r\n```\r\ns = pd.Series(['a', 'b', 'a'])\r\nsc = pd.Series(['a', 'b', 'a'], dtype='category')\r\ns.astype('category')\r\n# 0    a\r\n# 1    b\r\n# 2    a\r\n# dtype: category\r\n# Categories (2, object): [a, b]\r\n\r\n# # both ways yield the same result\r\nsc.equals(s.astype('category'))\r\n# True\r\n\r\n# # For Index ...\r\nt = pd.Index(s.values)\r\nt\r\n# Index(['a', 'b', 'a'], dtype='object')\r\n# # ... the direct constructor works\r\ntc = pd.Index(['a', 'b', 'a'], dtype='category')\r\n# # ... but not the conversion\r\nt.astype('category')\r\n# TypeError: data type \"category\" not understood\r\n```"},"comment":{"url":"https://api.github.com/repos/pandas-dev/pandas/issues/comments/385063371","html_url":"https://github.com/pandas-dev/pandas/issues/20843#issuecomment-385063371","issue_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20843","id":385063371,"user":{"login":"jschendel","id":5332445,"avatar_url":"https://avatars3.githubusercontent.com/u/5332445?v=4","gravatar_id":"","url":"https://api.github.com/users/jschendel","html_url":"https://github.com/jschendel","followers_url":"https://api.github.com/users/jschendel/followers","following_url":"https://api.github.com/users/jschendel/following{/other_user}","gists_url":"https://api.github.com/users/jschendel/gists{/gist_id}","starred_url":"https://api.github.com/users/jschendel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jschendel/subscriptions","organizations_url":"https://api.github.com/users/jschendel/orgs","repos_url":"https://api.github.com/users/jschendel/repos","events_url":"https://api.github.com/users/jschendel/events{/privacy}","received_events_url":"https://api.github.com/users/jschendel/received_events","type":"User","site_admin":false},"created_at":"2018-04-27T18:57:45Z","updated_at":"2018-04-27T18:57:45Z","author_association":"CONTRIBUTOR","body":"If I understand correctly, this looks to be fixed on master by #18677:\r\n\r\n```python\r\nIn [2]: pd.__version__\r\nOut[2]: '0.23.0.dev0+807.g563a6ad'\r\n\r\nIn [3]: idx = pd.Index(list('aba'))\r\n\r\nIn [4]: idx\r\nOut[4]: Index(['a', 'b', 'a'], dtype='object')\r\n\r\nIn [5]: idx.astype('category')\r\nOut[5]: CategoricalIndex(['a', 'b', 'a'], categories=['a', 'b'], ordered=False, dtype='category')\r\n```\r\n\r\nWith the same procedure failing on 0.22.0:\r\n```python\r\nIn [2]: pd.__version__\r\nOut[2]: '0.22.0'\r\n\r\nIn [3]: idx = pd.Index(list('aba'))\r\n\r\nIn [4]: idx\r\nOut[4]: Index(['a', 'b', 'a'], dtype='object')\r\n\r\nIn [5]: idx.astype('category')\r\n---------------------------------------------------------------------------\r\nTypeError: data type \"category\" not understood\r\n```"}}
{"actor":{"display_login":"jschendel"}}