DataFrameGroupBy.aggregate can not work with `tuple` as an argument
See original GitHub issueThe following code raises ValueError
grouped_df = df.groupby(group_by_attributes, as_index=False).aggregate(tuple)
Here is a more replicatable version:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(100, 3), columns=list('ABC'))
grouped_df = df.groupby(['A', 'B'], as_index=False).aggregate(tuple)
Problem description
The statement above does not work because tuple
is not a function. It throws:
ValueError: no results
Workaround
use the following groupby
statement instead
grouped_df = df.groupby(['A', 'B'], as_index=False).aggregate(lambda x: tuple(x))
This was issued as a result of the following discussion: https://github.com/PyCQA/pylint/issues/1709#issuecomment-341095096
Expected Output
Should be able to work without raising a ValueError
Output of pd.show_versions()
INSTALLED VERSIONS
commit: None python: 2.7.12.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-97-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: en_US.UTF-8 LANG: en_US.UTF-8 LOCALE: None.None
pandas: 0.20.3 pytest: None pip: 8.1.2 setuptools: 28.2.0 Cython: 0.24.1 numpy: 1.13.3 scipy: 0.18.1 xarray: None IPython: 5.4.1 sphinx: 1.4.8 patsy: 0.4.1 dateutil: 2.5.3 pytz: 2016.7 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 1.5.3 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: 1.0b10 sqlalchemy: None pymysql: None psycopg2: None jinja2: 2.8 s3fs: None pandas_gbq: None pandas_datareader: None
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
Hi @sinanonur, your example works in the newly released 0.21.0. Please upgrade when you can
Try removing and see what happens. When there’s a bug in our code, all bets are (almost) off 😄