-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: groupby.min has a side effect on groupby.apply #34656
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
Thanks, looks like the side effect happens here: pandas/pandas/core/groupby/groupby.py Line 990 in fc37087
Not sure if there would be an easy fix for this, other than the obvious workaround of not persisting the groupby object. |
There is a context manager |
@gshimansky interested in submitting a PR to fix? |
@WillAyd I can only workaround this problem by recreating groupby object |
The problem does seem to arise from calling import pandas as pd
df = pd.DataFrame(
{
"col1": [0, 1, 2, 3],
"col4": [17, 13, 16, 15],
"col5": [-4, -5, -6, -7],
}
)
by=["col4", "col5"]
apply_function = min
gb = df.groupby(by, as_index=True)
df1 = gb.apply(apply_function)
print(df1)
print(gb._group_selection)
gb._set_group_selection()
print(gb._group_selection)
df3 = gb.apply(apply_function)
print(df3) @WillAyd @dsaxton I am happy to raise a PR to fix, but first need clarification on what is the desired behaviour: should an initial call to Using the example above: gb = df.groupby(by, as_index=True)
result = gb.apply(apply_function) After running these two lines, should |
Looks like #34271 is the same issue. |
take |
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
Problem description
[this should explain why the current behaviour is a problem and why the expected output is a better solution]
In the code above two calls to
gb.apply(apply_function)
produce different output. The reason for this is thatgroupby.min
is called before 2ndapply
and makes its output different and incorrect.Expected Output
Expected that both calls to
gb.apply(apply_function)
produce the same output.Output of
pd.show_versions()
pandas : 1.0.4
numpy : 1.18.4
pytz : 2019.2
dateutil : 2.7.3
pip : 20.1.1
setuptools : 47.1.0
Cython : 0.29.17
pytest : 5.4.2
hypothesis : None
sphinx : None
blosc : None
feather : 0.4.1
xlsxwriter : None
lxml.etree : 4.5.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.12.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.1
matplotlib : 3.2.1
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : 0.13.2
pyarrow : 0.16.0
pytables : None
pytest : 5.4.2
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : 1.3.17
tables : 3.6.1
tabulate : None
xarray : 0.15.1
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : 0.46.0
The text was updated successfully, but these errors were encountered: