Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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 @@ -1169,6 +1169,7 @@ Sparse
- Bug in :class:`SparseDtype` for equal comparison with na fill value. (:issue:`54770`)
- Bug in :meth:`DataFrame.sparse.from_spmatrix` which hard coded an invalid ``fill_value`` for certain subtypes. (:issue:`59063`)
- Bug in :meth:`DataFrame.sparse.to_dense` which ignored subclassing and always returned an instance of :class:`DataFrame` (:issue:`59913`)
- Bug in :meth:`cumsum` for integer arrays Calling SparseArray.cumsum caused max recursion depth error. (:issue:`62669`)

ExtensionArray
^^^^^^^^^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,9 @@ def cumsum(self, axis: AxisInt = 0, *args, **kwargs) -> SparseArray:
raise ValueError(f"axis(={axis}) out of bounds")

if not self._null_fill_value:
if isinstance(self, SparseArray) and self.fill_value == 0:
# special case where we can avoid max recursion depth
return SparseArray(self.to_dense().cumsum())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not self._null_fill_value:
if isinstance(self, SparseArray) and self.fill_value == 0:
# special case where we can avoid max recursion depth
return SparseArray(self.to_dense().cumsum())
if not self._null_fill_value and self.fill_value != 0:

Copy link
Member

@rhshadrach rhshadrach Oct 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now this was wrong, but the previous implementation also did not handle the case

arr = pd.arrays.SparseArray([1.0, 0.0, np.nan, 3.0], fill_value=0.0)

correctly as it would have given the result [1.0, 1.0, nan, nan] whereas I believe docstring indicates the result should be [1.0, 1.0, nan, 4.0]. We can accomplish this by:

if not self._null_fill_value:
    return SparseArray(self.to_dense(), fill_value=np.nan).cumsum()

Note this changes the fill_value to NaN, but that adheres to the docstring. Anytime this method has been successful in previous versions, it was always returning an NA fill value regardless of the input fill value. So I think we should stick to that logic at least for now.

return SparseArray(self.to_dense()).cumsum()

return SparseArray(
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,20 @@ def test_setting_fill_value_fillna_still_works():
tm.assert_numpy_array_equal(result, expected)


def test_cumsum_integer_no_recursion():
# GH 62669: RecursionError in integer SparseArray.cumsum
arr = SparseArray([1, 2, 3])
result = arr.cumsum()
expected = SparseArray([1, 3, 6])
tm.assert_sp_array_equal(result, expected)

# Also test with some zeros interleaved
arr2 = SparseArray([0, 1, 0, 2])
result2 = arr2.cumsum()
expected2 = SparseArray([0, 1, 1, 3])
tm.assert_sp_array_equal(result2, expected2)


def test_setting_fill_value_updates():
arr = SparseArray([0.0, np.nan], fill_value=0)
arr.fill_value = np.nan
Expand Down
Loading