BUG: IndexError with pandas.DataFrame.cumsum where dtype=timedelta64[ns] · Issue #57956 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd

Example DataFrame from pandas.DataFrame.cumsum documentation

df = pd.DataFrame([[2.0, 1.0], [3.0, np.nan], [1.0, 0.0]], columns=list('AB'))

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

df.astype('timedelta64[ns]').cumsum()

These are fine

df.cumsum().astype('timedelta64[ns]') df.apply(lambda s: s.astype('timedelta64[ns]').cumsum()) df.astype('timedelta64[ns]').apply(lambda s: s.cumsum())

Issue Description

A cumulative sum along the columns of a DataFrame with timedelta64[ns] types results in an IndexError. Cumulative min and max work correctly, and product correctly results in a TypeError, since it's not supported for timedelta.

Cumulative sums work correctly for Series with timedelta64[ns] type, so this seems to be a bug in DataFrame.

This is perhaps related to #41720?

Expected Behavior

pandas.DataFrame.cumsum on a DataFrame with timedelta64[ns] columns should not erroneously raise IndexError and instead compute the cumulative sum, with the same effect as df.apply(lambda s: s.cumsum()).

Installed Versions

Installed Versions

INSTALLED VERSIONS
------------------
commit                : bdc79c146c2e32f2cab629be240f01658cfb6cc2
python                : 3.12.2.final.0
python-bits           : 64
OS                    : Darwin
OS-release            : 23.4.0
Version               : Darwin Kernel Version 23.4.0: Wed Feb 21 21:45:48 PST 2024; root:xnu-10063.101.15~2/RELEASE_ARM64_T8122
machine               : arm64
processor             : arm
byteorder             : little
LC_ALL                : None
LANG                  : en_US.UTF-8
LOCALE                : en_US.UTF-8

pandas                : 2.2.1
numpy                 : 1.26.4
pytz                  : 2024.1
dateutil              : 2.9.0.post0
setuptools            : None
pip                   : 24.0
Cython                : None
pytest                : None
hypothesis            : None
sphinx                : None
blosc                 : None
feather               : None
xlsxwriter            : None
lxml.etree            : None
html5lib              : None
pymysql               : None
psycopg2              : None
jinja2                : None
IPython               : 8.22.2
pandas_datareader     : None
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : None
bottleneck            : None
dataframe-api-compat  : None
fastparquet           : None
fsspec                : None
gcsfs                 : None
matplotlib            : 3.8.3
numba                 : None
numexpr               : None
odfpy                 : None
openpyxl              : None
pandas_gbq            : None
pyarrow               : None
pyreadstat            : None
python-calamine       : None
pyxlsb                : None
s3fs                  : None
scipy                 : 1.12.0
sqlalchemy            : None
tables                : None
tabulate              : None
xarray                : None
xlrd                  : None
zstandard             : None
tzdata                : 2024.1
qtpy                  : None
pyqt5                 : None