-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Description
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:
pandas/pandas/core/arrays/sparse/array.py
Lines 1573 to 1574 in 1863adb
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:
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