Skip to content

Cumsum not available to column of list-likes in groupby #40488

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

Closed
3 tasks done
rben01 opened this issue Mar 17, 2021 · 3 comments
Closed
3 tasks done

Cumsum not available to column of list-likes in groupby #40488

rben01 opened this issue Mar 17, 2021 · 3 comments
Labels
API - Consistency Internal Consistency of API/Behavior Duplicate Report Duplicate issue or pull request Enhancement Groupby

Comments

@rben01
Copy link

rben01 commented Mar 17, 2021

  • 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.

Code Sample, a copy-pastable example

df = pd.DataFrame({'a': [1,1,2], 'b': [[10],[11],[12]]})

# ok:
>>> df['b'].cumsum()
0            [10]
1        [10, 11]
2    [10, 11, 12]
Name: b, dtype: object

# error:
>>> df.groupby('a')['b'].cumsum()
DataError: No numeric types to aggregate

Problem description

Given that taking the cumsum of a column of list-likes is allowed, it should similarly be allowed when doing the same to a groupby object.

Expected Output

a
0        [10]
1    [10, 11]
2        [12]
Name: b, dtype: object

Workaround

This is only a partial workaround because it loses the index name, which is normally kept when doing a groupby.

df.groupby('a')['b'].apply(lambda col: col.cumsum())

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : b5958ee1999e9aead1938c0bba2b674378807b3d
python           : 3.8.7.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 20.4.0
Version          : Darwin Kernel Version 20.4.0: Fri Mar  5 03:57:04 PST 2021; root:xnu-7195.101.1~4/RELEASE_X86_64
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.1.5
numpy            : 1.19.4
pytz             : 2020.5
dateutil         : 2.8.1
pip              : None
setuptools       : 50.3.1.post0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : 4.5.2
html5lib         : 1.1
pymysql          : None
psycopg2         : None
jinja2           : 2.11.2
IPython          : 7.19.0
pandas_datareader: None
bs4              : 4.9.3
bottleneck       : 1.3.2
fsspec           : None
fastparquet      : None
gcsfs            : None
matplotlib       : 3.3.3
numexpr          : 2.7.2
odfpy            : None
openpyxl         : 3.0.5
pandas_gbq       : None
pyarrow          : None
pytables         : None
pyxlsb           : None
s3fs             : None
scipy            : 1.6.0
sqlalchemy       : 1.3.20
tables           : 3.6.1
tabulate         : 0.8.7
xarray           : None
xlrd             : 1.2.0
xlwt             : 1.3.0
numba            : None
@rben01 rben01 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 17, 2021
@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map Enhancement Groupby API - Consistency Internal Consistency of API/Behavior and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 20, 2021
@rhshadrach
Copy link
Member

rhshadrach commented Mar 20, 2021

Thanks for the report! It seems to me the workaround should preserve the original index at least with .transform (but I also expected .apply as well), however it does not. This aspect appears to be a bug to me. The index and index name appears to be preserved with both apply and transform.

If you're expecting the index to be 'a', that only occurs in a reduction, not a transform. In a transform, the original index of the DataFrame is preserved.

I'm +1 on supporting for non-numeric dtypes in cumsum, being consistent with sum.

@rhshadrach rhshadrach removed Apply Apply, Aggregate, Transform, Map Bug labels Mar 20, 2021
@mzeitlin11
Copy link
Member

#13992 is a similar request

@rhshadrach
Copy link
Member

Thanks for finding that @mzeitlin11. Closing as a duplicate and I've posted my thoughts there.

@rhshadrach rhshadrach added the Duplicate Report Duplicate issue or pull request label Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Duplicate Report Duplicate issue or pull request Enhancement Groupby
Projects
None yet
Development

No branches or pull requests

3 participants