-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: groupby.apply()
inconsistently indexes its return value with the groupby-cols
#44803
Comments
I just met the same problem, and did some test on the latest released version v1.3.5. It seems that the pandas/pandas/core/groupby/ops.py Lines 813 to 820 in 66e3805
And the truth value of pandas/pandas/core/groupby/groupby.py Lines 1003 to 1067 in 66e3805
I tried to disable the pandas/pandas/core/groupby/ops.py Lines 847 to 855 in 66e3805
and the value of SummaryI think there is a bug that the mutate checking of the fast apply(supported by |
I encounter a similar issue.
IMO, the most intuitive behavior is always adding group columns to index. The fact that 'transform' operations don't do it - was a major source of confustion for me and my colleagues. |
This is consistent on the master branch now (which will be reflected in the 1.4 release). This was probably most likely made consistent by #42992
Suppose could use a unit test to prevent a regression. |
take |
May I work on this? |
Yes! |
@pratimugale are you working on this? If not, I would like to take it |
@NumberPiOso you can take it |
take |
I did not understand if the output shown is the expected behaviour (#45476) . However, I will do the test to keep outputs similar, regardless of the results
|
Pending tasks from pandas-dev#45509 In line with issue pandas-dev#44803
Pending tasks from pandas-dev#45509 In line with issue pandas-dev#44803 Change test_groupby_copy from `pandas/tests/groupby/test_groupby.py` to `pandas/tests/groupby/test_apply_mutate.py`
Pending tasks from pandas-dev#45509 In line with issue pandas-dev#44803 Change test_groupby_copy from `pandas/tests/groupby/test_groupby.py` to `pandas/tests/groupby/test_apply_mutate.py`
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 master branch of pandas.
Reproducible Example
Issue Description
The above snippet produces the following output:
Where we can see that despite the return values of the
apply-func
being semantically identical (group
vs.group.copy()
), one ends up being indexed withage
andname
while the other only getsname
.Expected Behavior
I would have expected the two returned dataframes to be the same. I have a personal preference for the first option, with the
age
appearing in the index, but I think I would be able to work with both.I know other inconsistencies have been reported:
But IMHO never as clearly as this. I'm hoping this reproducer helps diagnose the issue more easily.
Interestingly, the difference only creeps up when
grouping_keys
is set toTrue
. Otherwise, we get the second output twice.Installed Versions
INSTALLED VERSIONS
commit : 945c9ed
python : 3.9.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.6-arch2-1
Version : #1 SMP PREEMPT Thu, 02 Dec 2021 15:47:09 +0000
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.4
numpy : 1.21.4
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.5.3
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: