Skip to content

BUG: Calling SparseArray.cumsum leads to infinite recursion #62669

@carlini

Description

@carlini

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

from pandas.arrays import SparseArray
import numpy as np

# Create a simple SparseArray with integer values
# Integer arrays default to fill_value=0
sparse = SparseArray([1, 2, 3])

print(f"SparseArray: {sparse}")
print(f"Fill value: {sparse.fill_value}")
print(f"_null_fill_value: {sparse._null_fill_value}")

# This should calculate cumulative sum [1, 3, 6]
# But it will cause RecursionError
try:
    result = sparse.cumsum()
    print(f"Cumsum result: {result}")
except RecursionError as e:
    print(f"\nRecursionError occurred!")
    print(f"Error: {e}")

Issue Description

The cumsum() method on SparseArray causes infinite recursion and crashes with RecursionError when the array has a non-null fill value, which is the default behavior for all integer SparseArrays (fill_value=0).

The offending code appears to be this:

if not self._null_fill_value:
return SparseArray(self.to_dense()).cumsum()

I discovered this bug as part of a project where we are use LLMs to search for bugs in popular open source repositories. Before filing this bug, we paid for three expert human reviewers to validate this bug, and I also manually validated the bug on my own machines. I am confident that the bug is real, I wrote and filed this report manually, and take responsibility for this bug report.

The LLM provided the following suggested fix that seems somewhat valid to me but I'm not familiar with this repo and so can't validate myself:

--- a/pandas/core/arrays/sparse/array.py
+++ b/pandas/core/arrays/sparse/array.py
@@ -1547,7 +1547,7 @@ class SparseArray(OpsMixin, PandasObject, ExtensionArray):
             raise ValueError(f"axis(={axis}) out of bounds")

         if not self._null_fill_value:
-            return SparseArray(self.to_dense()).cumsum()
+            return SparseArray(self.to_dense().cumsum())

         return SparseArray(
             self.sp_values.cumsum(),

Expected Behavior

It doesn't crash :)

Stacktrace provided below:

Traceback (most recent call last): File "", line 1, in result = sparse.cumsum() File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/arrays/sparse/array.py", line 1550, in cumsum return SparseArray(self.to_dense()).cumsum() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/arrays/sparse/array.py", line 1550, in cumsum return SparseArray(self.to_dense()).cumsum() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/arrays/sparse/array.py", line 1550, in cumsum return SparseArray(self.to_dense()).cumsum() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ [Previous line repeated 981 more times] File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/arrays/sparse/array.py", line 495, in __init__ self._dtype = SparseDtype(sparse_values.dtype, fill_value) ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/dtypes/dtypes.py", line 1689, in __init__ self._check_fill_value() ~~~~~~~~~~~~~~~~~~~~~~^^ File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/dtypes/dtypes.py", line 1777, in _check_fill_value if not can_hold_element(dummy, val): ~~~~~~~~~~~~~~~~^^^^^^^^^^^^ File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/dtypes/cast.py", line 1772, in can_hold_element np_can_hold_element(dtype, element) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^ File "/home/npc/pbt/agentic-pbt/envs/pandas_env/lib/python3.13/site-packages/pandas/core/dtypes/cast.py", line 1810, in np_can_hold_element info = np.iinfo(dtype) RecursionError: maximum recursion depth exceeded

Installed Versions

INSTALLED VERSIONS

commit : 4665c10
python : 3.13.2
python-bits : 64
OS : Linux
OS-release : 6.14.0-1017-gcp
Version : #18~24.04.1-Ubuntu SMP Tue Sep 23 17:51:44 UTC 2025
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : C.UTF-8

pandas : 2.3.2
numpy : 2.3.3
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 24.3.1
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : 6.139.1
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.4.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions