-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: transform("max") replaces all entries with NaN when applied after groupby with as_index=False #49834
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
This worked in 1.4.4, but I don't think that this should work. max is a reducer and should be used with agg and not transform. cc @rhshadrach Can you weigh in? |
related : #37093 |
Thanks for the report! Haven't run a git bisect but it will undoubtedly point to one of my PRs. Should be able to get a fix in for 1.5.3.
When given a reducer, transform will compute the result and then broadcast it to each group. E.g.
|
The result you want can be achieved by
|
@Verena1695 - we've been discussing if there should be an option in groupby for users to get the groups for a transform in the index (or columns) of the result. The question we have is if there are use cases for this - I've never had one in my experience. Do you have a use case where you want the keys in the result? If so, could you share any details on your use? |
Thanks for your comments. I discussed with the team and we came to the conclusion that we don't have a use case for having the groups in the index/columns of the result. |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
When using transform("max") after groupby with as_index=False, all entries in the original dataframe are converted to NaN. This is not only the case for "max" but also for "min" as well as "sum" or "mean" when having integers in the "value" column.
When using as_index=True instead, the transform works as expected, however, the "key" column does not appear as an index but simply disappears from the dataframe.
Expected Behavior
True
False
True
False
True
Installed Versions
INSTALLED VERSIONS
commit : 91111fd
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.104-linuxkit
Version : #1 SMP Thu Mar 17 17:08:06 UTC 2022
machine : x86_64
processor :
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.4
dateutil : 2.8.2
setuptools : 58.1.0
pip : 21.2.4
Cython : None
pytest : 7.2.0
hypothesis : 5.23.7
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.8.6
jinja2 : 3.1.2
IPython : 8.5.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.02.0
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0.2
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.0
snappy : None
sqlalchemy : 1.3.24
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : 0.14.0
tzdata : None
The text was updated successfully, but these errors were encountered: