Skip to content

Commit 3904711

Browse files
Aloqeelymroeschke
andauthored
Fix DataFrame.cumsum failing when dtype is timedelta64[ns] (#58028)
* Fix DataFrame.cumsum failing when dtype is timedelta64[ns] * Pre-commit * Remove comment Co-authored-by: Matthew Roeschke <[email protected]> * Update whatsnew --------- Co-authored-by: Abdulaziz Aloqeely <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 7f93018 commit 3904711

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Bug fixes
320320
~~~~~~~~~
321321
- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
322322
- Fixed bug in :meth:`.DataFrameGroupBy.median` where nat values gave an incorrect result. (:issue:`57926`)
323+
- Fixed bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`)
323324
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
324325
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
325326
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)

pandas/core/array_algos/datetimelike_accumulations.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def _cum_func(
4949
if not skipna:
5050
mask = np.maximum.accumulate(mask)
5151

52-
result = func(y)
52+
# GH 57956
53+
result = func(y, axis=0)
5354
result[mask] = iNaT
5455

5556
if values.dtype.kind in "mM":

pandas/tests/series/test_cumulative.py

+19
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ def test_cummin_cummax_datetimelike(self, ts, method, skipna, exp_tdi):
9191
result = getattr(ser, method)(skipna=skipna)
9292
tm.assert_series_equal(expected, result)
9393

94+
def test_cumsum_datetimelike(self):
95+
# GH#57956
96+
df = pd.DataFrame(
97+
[
98+
[pd.Timedelta(0), pd.Timedelta(days=1)],
99+
[pd.Timedelta(days=2), pd.NaT],
100+
[pd.Timedelta(hours=-6), pd.Timedelta(hours=12)],
101+
]
102+
)
103+
result = df.cumsum()
104+
expected = pd.DataFrame(
105+
[
106+
[pd.Timedelta(0), pd.Timedelta(days=1)],
107+
[pd.Timedelta(days=2), pd.NaT],
108+
[pd.Timedelta(days=1, hours=18), pd.Timedelta(days=1, hours=12)],
109+
]
110+
)
111+
tm.assert_frame_equal(result, expected)
112+
94113
@pytest.mark.parametrize(
95114
"func, exp",
96115
[

0 commit comments

Comments
 (0)