Skip to content

Commit 1488157

Browse files
authored
Disallow cumprod for timedelta (#50246)
1 parent 79b131e commit 1488157

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ Other API changes
485485
or :attr:`~DataFrame.iloc` (thus, ``df.loc[:, :]`` or ``df.iloc[:, :]``) now returns a
486486
new DataFrame (shallow copy) instead of the original DataFrame, consistent with other
487487
methods to get a full slice (for example ``df.loc[:]`` or ``df[:]``) (:issue:`49469`)
488+
- Disallow computing ``cumprod`` for :class:`Timedelta` object; previously this returned incorrect values (:issue:`50246`)
488489
-
489490

490491
.. ---------------------------------------------------------------------------

pandas/core/arrays/timedeltas.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,13 @@ def _accumulate(self, name: str, *, skipna: bool = True, **kwargs):
417417

418418
data = self._ndarray.copy()
419419

420-
if name in {"cumsum", "cumprod"}:
421-
# TODO: cumprod should not work here GH#48111
422-
func = np.cumsum if name == "cumsum" else np.cumprod
420+
if name == "cumsum":
421+
func = np.cumsum
423422
result = cast(np.ndarray, nanops.na_accum_func(data, func, skipna=skipna))
424423

425424
return type(self)._simple_new(result, freq=None, dtype=self.dtype)
425+
elif name == "cumprod":
426+
raise TypeError("cumprod not supported for Timedelta.")
426427

427428
else:
428429
return super()._accumulate(name, skipna=skipna, **kwargs)

pandas/tests/series/test_cumulative.py

+6
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,9 @@ def test_cummethods_bool_in_object_dtype(self, method, expected):
129129
ser = pd.Series([False, True, np.nan, False])
130130
result = getattr(ser, method)()
131131
tm.assert_series_equal(result, expected)
132+
133+
def test_cumprod_timedelta(self):
134+
# GH#48111
135+
ser = pd.Series([pd.Timedelta(days=1), pd.Timedelta(days=3)])
136+
with pytest.raises(TypeError, match="cumprod not supported for Timedelta"):
137+
ser.cumprod()

0 commit comments

Comments
 (0)