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

BUG: GroupBy.apply iterates over first group twice #12155

Closed
tomderuijter opened this issue Jan 27, 2016 · 4 comments · Fixed by #24748
Closed

BUG: GroupBy.apply iterates over first group twice #12155

tomderuijter opened this issue Jan 27, 2016 · 4 comments · Fixed by #24748

Comments

@tomderuijter
Copy link

When using the GroupBy.apply() method with a custom function handle, the first group is iterated over twice. Explicitly looping over each group and applying the function directly does not yield the error.

Error reproduction

>>> import pandas as pd
>>> df = pd.DataFrame({'one':[1,2,3,1,2,3],'two':[4,5,6,7,8,9]})
>>> df
   one  two
0    1    4
1    2    5
2    3    6
3    1    7
4    2    8
5    3    9

>>> gp = df.groupby('one')
>>> def foo(group):
...   print(group.iloc[[0]].index[0])
...
>>> gp.apply(foo)
0
0
1
2
Empty DataFrame
Columns: []
Index: []

>>> len(gp)
3

The example prints the row index of the first group member. The '0' is printed twice.

Versions:

>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.17.1
nose: None
pip: 8.0.1
setuptools: 19.4
Cython: None
numpy: 1.10.1
scipy: None
statsmodels: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
Jinja2: None
@tomderuijter tomderuijter changed the title GroupBy.apply iterates over first group twice BUG: GroupBy.apply iterates over first group twice Jan 27, 2016
@jreback
Copy link
Contributor

jreback commented Jan 27, 2016

See the big red warning box: http://pandas.pydata.org/pandas-docs/stable/groupby.html#flexible-apply

This is for efficiency and is an implementation detail.

@jreback
Copy link
Contributor

jreback commented Jan 27, 2016

xref is #2936

@jreback
Copy link
Contributor

jreback commented Jan 27, 2016

xref #10222

@amir-rafieian
Copy link

This bug is still exists in 0.25.3 and also 1.0.3

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.

3 participants