{"action":"opened","issue":{"url":"https://api.github.com/repos/pandas-dev/pandas/issues/20566","repository_url":"https://api.github.com/repos/pandas-dev/pandas","labels_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20566/labels{/name}","comments_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20566/comments","events_url":"https://api.github.com/repos/pandas-dev/pandas/issues/20566/events","html_url":"https://github.com/pandas-dev/pandas/issues/20566","id":310243352,"number":20566,"title":"BUG: When agg or reset_index to grouped DF, insert function causes ValueError.","user":{"login":"TakaakiFuruse","id":8233663,"avatar_url":"https://avatars2.githubusercontent.com/u/8233663?v=4","gravatar_id":"","url":"https://api.github.com/users/TakaakiFuruse","html_url":"https://github.com/TakaakiFuruse","followers_url":"https://api.github.com/users/TakaakiFuruse/followers","following_url":"https://api.github.com/users/TakaakiFuruse/following{/other_user}","gists_url":"https://api.github.com/users/TakaakiFuruse/gists{/gist_id}","starred_url":"https://api.github.com/users/TakaakiFuruse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TakaakiFuruse/subscriptions","organizations_url":"https://api.github.com/users/TakaakiFuruse/orgs","repos_url":"https://api.github.com/users/TakaakiFuruse/repos","events_url":"https://api.github.com/users/TakaakiFuruse/events{/privacy}","received_events_url":"https://api.github.com/users/TakaakiFuruse/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-31T14:08:11Z","updated_at":"2018-03-31T14:08:11Z","closed_at":null,"author_association":"NONE","body":"#### Code Sample, a copy-pastable example if possible\r\n\r\nCase 1\r\n```python\r\nimport pandas as pd\r\ndf = pd.DataFrame({\r\n               'a' : [1,1,1,2,2,2,3,3,3],\r\n               'b' : [1,2,3,4,5,6,7,8,9],\r\n})\r\ndf.groupby('a', as_index=False).agg({'a': 'count'})\r\n```\r\n\r\nCase 2\r\n\r\n```python\r\nimport pandas as pd\r\ndf = pd.DataFrame({\r\n               'a' : [1,1,1,2,2,2,3,3,3],\r\n               'b' : [1,2,3,4,5,6,7,8,9],\r\n})\r\ndf.groupby(['a']).agg({'a': 'count'}).reset_index(level=0)\r\n```\r\nIt returns \"ValueError: cannot insert a, already exists\" for both caes.\r\n\r\n#### Problem description\r\nSo far, I could found 2 functions which calls \"insert\" function without \"allow_duplicates=True\" option.\r\nOne is in  \"def _insert_inaxis_grouper_inplace\" from \"pandas/core/groupby.py\"\r\n```py\r\nresult.insert(0, name, lev)\r\n```\r\nand another is in \"def reset_index\" from \"pandas/core/frame.py\"\r\n```py\r\n new_obj.insert(0, name, level_values)\r\n```\r\n\r\nFor both cases, it result in ValueError as I wrote in Code section.\r\n\r\n#### Expected Output\r\nI think for both cases it should at least return some kind of dataframe without returning ValueError.\r\n\r\n1. It should add a new column even it was duplicated.\r\n2. It should add a new column with renaming it (say a_1 or a_x as new column).\r\n\r\nMy concern is\r\n\r\n1. Is this a bug? \r\n - Should we supposed to aggrecate for \"as_index=False\" dataframe or do \"reset_index\" to aggregated dataframe?\r\n - (i.e. Are there any other way to rename column before doing reset_index or agg?)\r\n2. If this were a bug, how could I fix it? \r\n  - I could add \"allow_duplicate=True\" option for both lines.\r\n  - Or, I could rename column by adding \"_x\" or \"_dup\" or any kind of suffix.\r\n\r\n#### Output of ``pd.show_versions()``\r\n\r\n<details>\r\n\r\nINSTALLED VERSIONS\r\n------------------\r\ncommit: None\r\npython: 3.6.4.final.0\r\npython-bits: 64\r\nOS: Linux\r\nOS-release: 4.8.0-53-generic\r\nmachine: x86_64\r\nprocessor: x86_64\r\nbyteorder: little\r\nLC_ALL: None\r\nLANG: ja_JP.UTF-8\r\nLOCALE: ja_JP.UTF-8\r\n\r\npandas: 0.22.0\r\npytest: 3.5.0\r\npip: 9.0.3\r\nsetuptools: 39.0.1\r\nCython: 0.28.1\r\nnumpy: 1.14.2\r\nscipy: 1.0.1\r\npyarrow: None\r\nxarray: None\r\nIPython: 6.2.1\r\nsphinx: 1.7.2\r\npatsy: 0.5.0\r\ndateutil: 2.7.2\r\npytz: 2018.3\r\nblosc: None\r\nbottleneck: 1.2.1\r\ntables: 3.4.2\r\nnumexpr: 2.6.4\r\nfeather: None\r\nmatplotlib: 2.2.2\r\nopenpyxl: 2.5.1\r\nxlrd: 1.1.0\r\nxlwt: 1.3.0\r\nxlsxwriter: 1.0.2\r\nlxml: 4.2.1\r\nbs4: 4.6.0\r\nhtml5lib: 1.0.1\r\nsqlalchemy: 1.2.5\r\npymysql: None\r\npsycopg2: None\r\njinja2: 2.10\r\ns3fs: None\r\nfastparquet: None\r\npandas_gbq: None\r\npandas_datareader: None\r\n</details>"}}
{"actor":{"display_login":"TakaakiFuruse"}}