-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Multiple lambdas for the same column return KeyError in DataFrameGroupBy.agg with named aggregation #27519
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
Comments
We would either need to mangle both pandas/pandas/core/groupby/generic.py Line 220 in 86049dd
here : pandas/pandas/core/groupby/generic.py Lines 263 to 265 in 86049dd
Note that the mangling will be a bit different, since we're mangling names (strings), not the names of lambda functions. |
We may also be able to do the mangling earlier on, so that the |
I think I am still seeing this in 0.25.3, the fix should be in that right? |
I am also still seeing this issue in 0.25.3. import pandas as pd
dft = pd.DataFrame({"A": [1, 2, 3, 4, 1, 2, 4],
"B": [1, 1, 1, 2, 2, 3, 3]})
dft.groupby("B").agg(A_two=pd.NamedAgg(column="A", aggfunc=lambda x: x.eq(2).sum()),
A_ones=pd.NamedAgg(column="A", aggfunc=lambda x: x.eq(1).sum())) Still results in:
pd.show_versions()
|
Hi, @jinlow @akdor1154 This issue was fixed by #27921 but hasn't been released yet, and will be released in 1.0 version. So feel free to test this feature once 1.0 is released. Best |
Multiple lambdas for the same column return
KeyError
inDataFrameGroupBy.agg
Problem description
When using the new groupby aggregation with relabeling API in pandas 0.25.0, a
KeyError
is raised when the same source column is used with multiple lambdas, as in the example above. This issue isn't present when using multiple lambdas withSeriesGroupBy
, as in the release notes.@TomAugspurger notes also that in
DataFrameGroupby.aggregate
,order
needs to be mangled too.Expected Output
Bonus related issue
If the applied function has the same name, a
SpecificationError
is raised with the messageFunction names must be unique, found multiple named mean
, even though the kwargs are different:(Obviously this is a silly example, but I encountered it having defined a closure for
np.percentile
to get around the lambda issue!)Output of
pd.show_versions()
INSTALLED VERSIONS
commit : None
python : 3.7.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 0.25.0
numpy : 1.16.4
pytz : 2019.1
dateutil : 2.8.0
pip : 19.1.1
setuptools : 41.0.1
Cython : None
pytest : None
hypothesis : None
sphinx : 2.0.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.2 (dt dec pq3 ext lo64)
jinja2 : 2.10.1
IPython : 7.5.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
s3fs : None
scipy : 1.3.0
sqlalchemy : 1.3.3
tables : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
The text was updated successfully, but these errors were encountered: