diff raises RecursionError when n is around 1000 or more
I maintainer di solito rispondono entro 1 giorno
Una pull request collegata è già stata integrata.
- #11704 di @headtr1ck — integrata
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 72/100
Direzione di ricerca
Inizia da xarray/core/dataset.py, in Dataset.diff, dove return difference.diff(dim, n - 1) viene eseguito una volta per ogni ordine di differenza (intorno alla riga 8103 del traceback). Inizia da lì e sostituisci l'auto-chiamata con un ciclo che applica la differenza di un singolo ordine n volte. È completato quando da.diff("x", n=1200) su un array di 3.000 valori restituisce 1.800 valori, come numpy.diff, e i test esistenti su diff passano ancora.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happened?
While testing an experimental version of Perch, we found the following and figured it was worth a report.
DataArray.diff and Dataset.diff raise RecursionError once n gets near Python's recursion limit (1000 by default). n=900 works and n=1000 fails.
What did you expect to happen?
The same result numpy.diff gives. For 3,000 values and n=1200, that is 1,800 values.
Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!
import numpy as np
import xarray as xr
xr.show_versions()
values = np.arange(3000.0) ** 2
da = xr.DataArray(values, dims="x")
print(np.diff(values, n=1200).shape) # (1800,)
print(da.diff("x", n=900).shape) # (2100,)
da.diff("x", n=1200) # RecursionError
Steps to reproduce
Save the example as issue.py and run uv run issue.py.
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Traceback (most recent call last):
File "issue.py", line 21, in <module>
da.diff("x", n=1200) # RecursionError
~~~~~~~^^^^^^^^^^^^^
File ".../site-packages/xarray/core/dataarray.py", line 5167, in diff
ds = self._to_temp_dataset().diff(n=n, dim=dim, label=label)
File ".../site-packages/xarray/core/dataset.py", line 8103, in diff
return difference.diff(dim, n - 1)
~~~~~~~~~~~~~~~^^^^^^^^^^^^
File ".../site-packages/xarray/core/dataset.py", line 8103, in diff
return difference.diff(dim, n - 1)
~~~~~~~~~~~~~~~^^^^^^^^^^^^
[Previous line repeated many more times]
...
File ".../site-packages/xarray/core/indexing.py", line 501, in __init__
k = as_integer_slice(k)
File ".../site-packages/xarray/core/indexing.py", line 454, in as_integer_slice
start = as_integer_or_none(value.start)
RecursionError: maximum recursion depth exceeded
Anything else we need to know?
Dataset.diff handles n > 1 by calling itself with n - 1, so each order of difference adds a stack frame:
if n > 1:
return difference.diff(dim, n - 1)
A loop doing the same thing n times would avoid the limit.
Environment
INSTALLED VERSIONS
------------------
commit: None
python: 3.14.2 (main, Dec 9 2025, 19:29:30) [Clang 21.1.4 ]
python-bits: 64
OS: Darwin
OS-release: 25.2.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: None
LOCALE: ('C', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: 4.9.3
xarray: 2026.9.1.dev52+g07f7270ee
pandas: 3.0.6
numpy: 2.5.3
scipy: 1.18.1
netCDF4: 1.7.4
pydap: 3.5.11
h5netcdf: 1.8.1
h5py: 3.16.0
zarr: 3.4.1
cftime: 1.6.6
nc_time_axis: 1.4.1
iris: None
bottleneck: 1.6.0
dask: 2026.8.0
distributed: 2026.8.0
matplotlib: 3.11.2
cartopy: 0.26.0
seaborn: 0.13.2
numbagg: 0.9.6
fsspec: 2026.9.0
cupy: None
pint: None
sparse: 0.19.2
flox: 0.11.2
numpy_groupies: 0.13.0
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
- Lingua principale
- Python
- Stelle
- 4.2k
- Fork
- 1.4k
- Merge medio
- 1g 23h
- PR unite (30g)
- 108
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pydata/xarray
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)Forse già presa @d-chambers l’ha presa 30 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
pydata/xarray#11598 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
DataTree constructor error message says dict even though dict is not acceptedForse già presa @aryansk l’ha presa 70 giorni fa. Apertabug contrib-good-first-issue topic-DataTree topic-error reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pydata/xarray#11514 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Use `zarr.Array.async_array`Forse già presa @0xrutabaga l’ha presa 14 giorni fa. Apertatopic-zarr
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
needs triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
pydata/xarray#10800 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Can't call open_mfdataset without creating chunked dask arraysForse già presa @Illviljan l’ha presa 1895 giorni fa. Apertabug io topic-chunked-arrays
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pydata/xarray#9038 · 4 commenti · 2 reazioni ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pydata/xarray
Issue simili
-
Uninstall guide omits the CloudWatch log groups AWS CodeBuild creates for useCodeBuild pipelinesApertabug documentation
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
awslabs/visual-asset-management-system#552 ·
I maintainer di solito rispondono entro 1 giorno
-
netbox status: needs triage type: documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 76/100
netbox-community/netbox#23460 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
awslabs/threat-composer#362 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
ansys/pyansys-common-mcp#213 ·
I maintainer di solito rispondono entro 3 giorni