Skip to content

Commit aa5726a

Browse files
Sparse array cumsum leads to infinite recursion 62669 (#62703)
Co-authored-by: Santhosh Kumar Bethi <santhosh.bhethi.com> Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Co-authored-by: Richard Shadrach <rhshadrach@gmail.com>
1 parent 0f3f89d commit aa5726a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,6 +1312,7 @@ Sparse
13121312
- Bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
13131313
- Bug in :meth:`DataFrame.sparse.from_spmatrix` which hard coded an invalid ``fill_value`` for certain subtypes. (:issue:`59063`)
13141314
- Bug in :meth:`DataFrame.sparse.to_dense` which ignored subclassing and always returned an instance of :class:`DataFrame` (:issue:`59913`)
1315+
- Bug in :meth:`cumsum` for integer arrays Calling SparseArray.cumsum caused max recursion depth error. (:issue:`62669`)
13151316

13161317
ExtensionArray
13171318
^^^^^^^^^^^^^^

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ def cumsum(self, axis: AxisInt = 0, *args, **kwargs) -> SparseArray:
15691569
raise ValueError(f"axis(={axis}) out of bounds")
15701570

15711571
if not self._null_fill_value:
1572-
return SparseArray(self.to_dense()).cumsum()
1572+
return SparseArray(self.to_dense(), fill_value=np.nan).cumsum()
15731573

15741574
return SparseArray(
15751575
self.sp_values.cumsum(),

pandas/tests/arrays/sparse/test_array.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,28 @@ def test_setting_fill_value_fillna_still_works():
363363
tm.assert_numpy_array_equal(result, expected)
364364

365365

366+
def test_cumsum_integer_no_recursion():
367+
# GH 62669: RecursionError in integer SparseArray.cumsum
368+
arr = SparseArray([1, 2, 3])
369+
result = arr.cumsum()
370+
expected = SparseArray([1, 3, 6], fill_value=np.nan)
371+
tm.assert_sp_array_equal(result, expected)
372+
373+
# Also test with some zeros interleaved
374+
arr2 = SparseArray([0, 1, 0, 2])
375+
result2 = arr2.cumsum()
376+
expected2 = SparseArray([0, 1, 1, 3], fill_value=np.nan)
377+
tm.assert_sp_array_equal(result2, expected2)
378+
379+
380+
def test_cumsum_float_fill_value_zero():
381+
# GH 62669
382+
arr = pd.arrays.SparseArray([1.0, 0.0, np.nan, 3.0], fill_value=0.0)
383+
result = arr.cumsum()
384+
expected = SparseArray([1.0, 1.0, None, 4.0], fill_value=np.nan)
385+
tm.assert_sp_array_equal(result, expected)
386+
387+
366388
def test_setting_fill_value_updates():
367389
arr = SparseArray([0.0, np.nan], fill_value=0)
368390
arr.fill_value = np.nan

0 commit comments

Comments
 (0)