DataFrameGroupBy.aggregate can not work with tuple as an argument · Issue #18079 · pandas-dev/pandas (original) (raw)

The 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: pylint-dev/pylint#1709 (comment)

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