Skip to content
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

DataFrameGroupBy.first() affects group without assignment #28523

Closed
Koojav opened this issue Sep 19, 2019 · 4 comments
Closed

DataFrameGroupBy.first() affects group without assignment #28523

Koojav opened this issue Sep 19, 2019 · 4 comments

Comments

@Koojav
Copy link

Koojav commented Sep 19, 2019

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar'], 'B' : [1, 2, 3, 4]})
grouped = df.groupby('A')
print(grouped.apply(lambda x: x))
grouped.first()
print(grouped.apply(lambda x: x))

Output:

     A  B
0  foo  1
1  bar  2
2  foo  3
3  bar  4

   B
0  1
1  2
2  3
3  4

Problem description

The invoking method, without re-assignment of result or specifying that it should work in place, should not affect the object upon which it is being called.

Expected Output

     A  B
0  foo  1
1  bar  2
2  foo  3
3  bar  4

     A  B
0  foo  1
1  bar  2
2  foo  3
3  bar  4

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-62-generic
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0.25.0
numpy : 1.17.0
pytz : 2019.2
dateutil : 2.8.0
pip : 19.2.1
setuptools : 40.6.3
Cython : 0.27.3
pytest : 3.0.7
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.2.6
html5lib : None
pymysql : None
psycopg2 : 2.7.3.2 (dt dec pq3 ext lo64)
jinja2 : 2.10
IPython : 7.2.0
pandas_datareader: None
bs4 : 4.6.3
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.2.6
matplotlib : 2.0.2
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : 0.11.0
pyarrow : 0.13.0
pytables : None
s3fs : None
scipy : 1.0.0
sqlalchemy : 1.3.7
tables : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None

@TomAugspurger
Copy link
Contributor

Can you check where things go wrong? We do have a mutated property on BaseGrouper. That's where I would start looking.

@WillAyd
Copy link
Member

WillAyd commented Sep 19, 2019

This affects a few functions like sum, prod, min, max, first, last, nth, and maybe more.

I think the issue is a call to self._set_group_selection() without resetting that selection after the fact. There is a _group_selection_context context manager that should be used instead.

@Koojav interested in trying a PR to fix?

@WillAyd WillAyd modified the milestones: 1.0, Contributions Welcome Sep 19, 2019
@WillAyd WillAyd added the Bug label Sep 19, 2019
@jreback
Copy link
Contributor

jreback commented Sep 19, 2019

i created an issue for this already but may have to search for it

@rhshadrach
Copy link
Member

Duplicate of #34656 and closed by #35314

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants