Skip to content

Fix DataFrame.cumsum failing when dtype is timedelta64[ns] #58028

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

Merged
merged 4 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ Bug fixes
~~~~~~~~~
- Fixed bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
- Fixed bug in :meth:`.DataFrameGroupBy.median` where nat values gave an incorrect result. (:issue:`57926`)
- Fixed bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`)
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Fixed bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Fixed bug in :meth:`DataFrame.update` bool dtype being converted to object (:issue:`55509`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/array_algos/datetimelike_accumulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def _cum_func(
if not skipna:
mask = np.maximum.accumulate(mask)

result = func(y)
# GH 57956
result = func(y, axis=0)
result[mask] = iNaT

if values.dtype.kind in "mM":
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/series/test_cumulative.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ def test_cummin_cummax_datetimelike(self, ts, method, skipna, exp_tdi):
result = getattr(ser, method)(skipna=skipna)
tm.assert_series_equal(expected, result)

def test_cumsum_datetimelike(self):
# GH#57956
df = pd.DataFrame(
[
[pd.Timedelta(0), pd.Timedelta(days=1)],
[pd.Timedelta(days=2), pd.NaT],
[pd.Timedelta(hours=-6), pd.Timedelta(hours=12)],
]
)
result = df.cumsum()
expected = pd.DataFrame(
[
[pd.Timedelta(0), pd.Timedelta(days=1)],
[pd.Timedelta(days=2), pd.NaT],
[pd.Timedelta(days=1, hours=18), pd.Timedelta(days=1, hours=12)],
]
)
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"func, exp",
[
Expand Down