{"action":"created","issue":{"url":"https://api.github.com/repos/pydata/pandas/issues/13913","repository_url":"https://api.github.com/repos/pydata/pandas","labels_url":"https://api.github.com/repos/pydata/pandas/issues/13913/labels{/name}","comments_url":"https://api.github.com/repos/pydata/pandas/issues/13913/comments","events_url":"https://api.github.com/repos/pydata/pandas/issues/13913/events","html_url":"https://github.com/pydata/pandas/issues/13913","id":169502070,"number":13913,"title":"Groupby apply using a custom function displays incorrect arguments (DF)","user":{"login":"bhagerman00","id":15328599,"avatar_url":"https://avatars.githubusercontent.com/u/15328599?v=3","gravatar_id":"","url":"https://api.github.com/users/bhagerman00","html_url":"https://github.com/bhagerman00","followers_url":"https://api.github.com/users/bhagerman00/followers","following_url":"https://api.github.com/users/bhagerman00/following{/other_user}","gists_url":"https://api.github.com/users/bhagerman00/gists{/gist_id}","starred_url":"https://api.github.com/users/bhagerman00/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bhagerman00/subscriptions","organizations_url":"https://api.github.com/users/bhagerman00/orgs","repos_url":"https://api.github.com/users/bhagerman00/repos","events_url":"https://api.github.com/users/bhagerman00/events{/privacy}","received_events_url":"https://api.github.com/users/bhagerman00/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-08-05T00:07:55Z","updated_at":"2016-08-05T03:02:04Z","closed_at":"2016-08-05T00:12:24Z","body":"#### Code Sample, a copy-pastable example if possible\r\n```\r\ndef func3(df):\r\n    print(df)\r\n    df['Mean'] = np.mean(df['Data'].values)\r\n    out = df[['Mean', 'Group']].iloc[0, :].copy(deep=True)\r\n    return out\r\n\r\nvar1 = [1, 2, 3]\r\ngrp_var = ['Group1', 'Group2', 'Group2']\r\ndf_test = pd.DataFrame.from_dict({'Data': var1, 'Group': grp_var})\r\n\r\n# Groupby test\r\ndf_grp = df_test.groupby(['Group'])\r\ndf_out = df_grp.apply(func3)\r\n```\r\n\r\n#### Surprising Print Output\r\n```\r\n   Data   Group\r\n0     1  Group1\r\n   Data   Group\r\n0     1  Group1\r\n   Data   Group  Mean\r\n1     2  Group2   1.0\r\n2     3  Group2\r\n   Data   Group\r\n0     1  Group1\r\n   Data   Group\r\n1     2  Group2\r\n2     3  Group2\r\n\r\nIn [31]: df_out\r\nOut[31]:\r\n        Mean   Group\r\nGroup\r\nGroup1   1.0  Group1\r\nGroup2   2.5  Group2\r\n```\r\n#### output of ``pd.show_versions()``\r\n```\r\nINSTALLED VERSIONS\r\n------------------\r\ncommit: None\r\npython: 2.7.12.final.0\r\npython-bits: 64\r\nOS: Darwin\r\nOS-release: 14.5.0\r\nmachine: x86_64\r\nprocessor: i386\r\nbyteorder: little\r\nLC_ALL: None\r\nLANG: en_US.UTF-8\r\n\r\npandas: 0.18.1\r\nnose: 1.3.7\r\npip: 8.1.2\r\nsetuptools: 23.0.0\r\nCython: 0.24.1\r\nnumpy: 1.11.1\r\nscipy: 0.18.0\r\nstatsmodels: 0.6.1\r\nxarray: 0.7.2\r\nIPython: 4.2.0\r\nsphinx: None\r\npatsy: 0.4.1\r\ndateutil: 2.5.2\r\npytz: 2016.4\r\nblosc: None\r\nbottleneck: 1.0.0\r\ntables: None\r\nnumexpr: 2.6.1\r\nmatplotlib: 1.5.2\r\nopenpyxl: None\r\nxlrd: None\r\nxlwt: None\r\nxlsxwriter: None\r\nlxml: None\r\nbs4: None\r\nhtml5lib: None\r\nhttplib2: None\r\napiclient: None\r\nsqlalchemy: None\r\npymysql: None\r\npsycopg2: None\r\njinja2: 2.8\r\nboto: 2.40.0\r\npandas_datareader: None\r\n```\r\n#### Summary\r\nThe final output is correct, but the intermediate argument calls appear to be referencing incorrect data frames. There are two groups to iterate over, but 5 statements are printed, one of which contains a new column called 'Mean' and is printed under Group2, but the mean value is for Group1. This seems to occur when a pandas series object is returned, and makes it difficult to interactively evaluate/debug groupby calls using custom arguments. The printed DF's should match the DF for the given group being processed. Is this known behavior? "},"comment":{"url":"https://api.github.com/repos/pydata/pandas/issues/comments/237743795","html_url":"https://github.com/pydata/pandas/issues/13913#issuecomment-237743795","issue_url":"https://api.github.com/repos/pydata/pandas/issues/13913","id":237743795,"user":{"login":"ankur-gupta","id":7110058,"avatar_url":"https://avatars.githubusercontent.com/u/7110058?v=3","gravatar_id":"","url":"https://api.github.com/users/ankur-gupta","html_url":"https://github.com/ankur-gupta","followers_url":"https://api.github.com/users/ankur-gupta/followers","following_url":"https://api.github.com/users/ankur-gupta/following{/other_user}","gists_url":"https://api.github.com/users/ankur-gupta/gists{/gist_id}","starred_url":"https://api.github.com/users/ankur-gupta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ankur-gupta/subscriptions","organizations_url":"https://api.github.com/users/ankur-gupta/orgs","repos_url":"https://api.github.com/users/ankur-gupta/repos","events_url":"https://api.github.com/users/ankur-gupta/events{/privacy}","received_events_url":"https://api.github.com/users/ankur-gupta/received_events","type":"User","site_admin":false},"created_at":"2016-08-05T03:02:04Z","updated_at":"2016-08-05T03:02:04Z","body":"I am concerned about the print output:\r\n```\r\n   Data   Group  Mean\r\n1     2  Group2   1.0\r\n2     3  Group2\r\n```\r\nThis dataframe doesn't actually exist in the GroupBy object list but it is still being passed to the custom function.\r\n\r\nI encounter datasets in which the use of custom apply functions is very helpful and straightforward. If the custom apply functions are not recommended, I would be happy to switch to a better alternative. What would you recommend as an alternative ?\r\n\r\n\r\n\r\n "}}
{"actor":{"display_login":"ankur-gupta"}}