Skip to content

API: Named aggregations for groupby.transform() #32318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Feb 28, 2020 · 1 comment
Closed

API: Named aggregations for groupby.transform() #32318

ghost opened this issue Feb 28, 2020 · 1 comment
Labels
Apply Apply, Aggregate, Transform, Map Enhancement Groupby

Comments

@ghost
Copy link

ghost commented Feb 28, 2020

Code Sample, a copy-pastable example if possible

In [2]: df = pd.DataFrame({
   ...:     'A': [1, 1, 2, 2, 3, 3],
   ...:     'B': [4, 5, 6, 7, 8, 9]
   ...: })

In [3]: df
Out[3]:
   A  B
0  1  4
1  1  5
2  2  6
3  2  7
4  3  8
5  3  9

In [4]: df.groupby('A').agg(maximum=('B', 'max'))
Out[4]:
   maximum
A
1        5
2        7
3        9

In [5]: df.groupby('A').transform(maximum=('B', 'max'))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-10080a262c70> in <module>
----> 1 df.groupby('A').transform(maximum=('B', 'max'))

TypeError: transform() missing 1 required positional argument: 'func'

In [6]: df['maximum'] = df.groupby('A')['B'].transform('max')

In [7]: df
Out[7]:
   A  B  maximum
0  1  4        5
1  1  5        5
2  2  6        7
3  2  7        7
4  3  8        9
5  3  9        9

Problem description

Named aggregation streamlines the process of creating columns as a result of groupby.agg(). A similar (identical?) API for groupby.transform() would provide the same benefit when creating new columns from groupby.transform().

This issue may be related to #28380.

Expected Output

In [2]: df = pd.DataFrame({
   ...:     'A': [1, 1, 2, 2, 3, 3],
   ...:     'B': [4, 5, 6, 7, 8, 9]
   ...: })

In [3]: df.groupby('A').transform(maximum=('B', 'max'))
Out[3]:
   A  B  maximum
0  1  4        5
1  1  5        5
2  2  6        7
3  2  7        7
4  3  8        9
5  3  9        9

Output of pd.show_versions()

In [8]: pd.show_versions()
C:\Users\dwales\AppData\Local\Continuum\anaconda3\lib\site-packages\fastparquet\dataframe.py:5: FutureWarning: pandas.core.index is deprecated and will be removed in a future version. The public classes are available in the top-level namespace.
from pandas.core.index import CategoricalIndex, RangeIndex, Index, MultiIndex

INSTALLED VERSIONS

commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None

pandas : 1.0.1
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.2.0.post20200210
Cython : 0.29.15
pytest : 5.3.5
hypothesis : 5.4.1
sphinx : 2.4.0
blosc : None
feather : None
xlsxwriter : 1.2.7
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.8.4 (dt dec pq3 ext lo64)
jinja2 : 2.11.1
IPython : 7.12.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : 1.3.1
fastparquet : 0.3.2
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 0.13.0
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.3.13
tables : 3.6.1
tabulate : 0.8.3
xarray : 0.15.0
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.7
numba : 0.48.0

@jbrockmendel jbrockmendel added API Design Apply Apply, Aggregate, Transform, Map Groupby labels Sep 1, 2020
@mroeschke
Copy link
Member

Thanks for the request but it appears this hasn't gotten traction in a while so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Enhancement Groupby
Projects
None yet
Development

No branches or pull requests

2 participants