Skip to content
Open
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 @@ -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())
Comment on lines +1574 to +1576
Copy link
Member

Choose a reason for hiding this comment

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

This fix seems to specific. Additionally especially for the open issue, it doesn't appear to_dense is needed

Copy link
Contributor Author

@santhoshbethi santhoshbethi Oct 15, 2025

Choose a reason for hiding this comment

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

Yeah, This fix is very specific to given use case.

       ` if isinstance(self, SparseArray) and self.fill_value == 0:
            # special case where we can avoid max recursion depth
            return SparseArray(self.cumsum())

If I don't add I am getting sameError: maximum recursion depth exceeded`

Choose a reason for hiding this comment

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

I think the fix being specific is fine. It is basically using the same "trick" that the code was previously using for float arrays with nan fill, which was that if it is non-nan fill, densify and resparse so now the fill is nan, and call .cumsum() again. So it recurses once. This trick broke for integer arrays because when you do the densify and reparsing, the default fill is 0. This fix now catches that case ensuring that int arrays also only recurse once.

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