Skip to content

groupby cumsum with axis=1 computes cumprod #13993

Closed
@agraboso

Description

@agraboso

Code sample

import pandas as pd

df = pd.DataFrame({'A': range(6), 'B': range(6)})
df.groupby('A').cumsum(axis=1)

Output

   A   B
0  0   0
1  1   1
2  2   4
3  3   9
4  4  16
5  5  25

Expected Output

   A   B
0  0   0
1  1   2
2  2   4
3  3   6
4  4   8
5  5  10

The culprit

A single line in pandas.core.groupby:

def cumsum(self, axis=0, *args, **kwargs):
    """Cumulative sum for each group"""
    nv.validate_groupby_func('cumsum', args, kwargs)
    if axis != 0:
        return self.apply(lambda x: x.cumprod(axis=axis))    # <==== should be `cumsum`

    return self._cython_transform('cumsum')

(the result of a copy-paste oversight in this commit)

output of pd.show_versions()

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

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: None
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: None
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions