Skip to content

Named Aggregation : Can't use multiple lambda on the same column #30536

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
alapini opened this issue Dec 29, 2019 · 0 comments
Closed

Named Aggregation : Can't use multiple lambda on the same column #30536

alapini opened this issue Dec 29, 2019 · 0 comments

Comments

@alapini
Copy link

alapini commented Dec 29, 2019

Note : I read #28467 before posting this

Code Sample, a copy-pastable example if possible

Reproduction :

import numpy as np
import pandas as pd
print(pd.__version__) # 0.25.3
animals = pd.DataFrame({'kind': ['cat', 'dog', 'cat', 'dog'],
                            'height': [9.1, 6.0, 9.5, 34.0],
                            'weight': [7.9, 7.5, 9.9, 198.0]})
aggegators = {
    'perc80' : ('height', lambda x:np.percentile(x, q=.80)),
    'perc90' : ('height', lambda x:np.percentile(x, q=.90)),
}
animals.groupby('kind').agg(**aggegators)

Problem description

Applying multiple unnamed aggregation functions to the same column throws the exception below, but may not, I think.
"None of [MultiIndex([('height', '<lambda>'),\n ('height', '<lambda>')],\n )] are in the [columns]"

Expected Output

image

Code without unnamed functions works fine

import numpy as np
import pandas as pd
print(pd.__version__) # 0.25.3
animals = pd.DataFrame({'kind': ['cat', 'dog', 'cat', 'dog'],
                            'height': [9.1, 6.0, 9.5, 34.0],
                            'weight': [7.9, 7.5, 9.9, 198.0]})

def perc80(x):
    return np.percentile(x,q=.80)

def perc90(x):
    return np.percentile(x,q=.90)

aggegators2 = {
    'perc80' : ('height', perc80),
    'perc90' : ('height', perc90),
}
animals.groupby('kind').agg(**aggegators2)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-18362-Microsoft
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.3
numpy : 1.17.4
pytz : 2019.3
dateutil : 2.8.1
pip : 19.3.1
setuptools : 42.0.2.post20191203
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.10.2
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@alapini alapini closed this as completed Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant