Skip to content

groupby(..).apply() has inconsistent behavior when there is only one group #13121

Closed
@smoofra

Description

@smoofra

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({'a':['foo', 'foo', 'bar'], 'b':[1,1,1]}).set_index('a')
def f(df):
    print '============'
    print df
    print
    print(pd.DataFrame(df.groupby(level='a').apply(lambda df: df['b'].cumsum())))

f(df)
f(df[df.index == 'foo'])

Actual Output

============
     b
a     
foo  1
foo  1
bar  1

         b
a   a     
bar bar  1
foo foo  1
    foo  2
============
     b
a     
foo  1
foo  1

a    foo  foo
a            
foo    1    2

Expected Output

============
     b
a     
foo  1
foo  1
bar  1

         b
a   a     
bar bar  1
foo foo  1
    foo  2
============
     b
a     
foo  1
foo  1

         b
a   a     
foo foo  1
    foo  2


output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.10.final.0
python-bits: 64
OS: Darwin
OS-release: 15.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.17.1
nose: 1.3.4
pip: 8.1.1
setuptools: 18.0.1
Cython: None
numpy: 1.9.2
scipy: 0.15.1
statsmodels: 0.6.1
IPython: 4.0.0
sphinx: None
patsy: 0.4.1
dateutil: 2.2
pytz: 2014.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.4.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.12
pymysql: None
psycopg2: None
Jinja2: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Duplicate ReportDuplicate issue or pull requestGroupbyReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions