From 86887a6c34fcb7fb7b88d30108986ca3e62c8dce Mon Sep 17 00:00:00 2001 From: Bounty Hunter Date: Tue, 23 Jun 2026 01:22:23 +0000 Subject: [PATCH] Fix: handle num=1 in RangeIndex.linspace to match numpy behavior When num=1, RangeIndex.linspace raised ZeroDivisionError because it divided by (num - 1). NumPy's linspace returns a single-element array at the start value in this case. This fix aligns the behavior. Fixes #11397 --- xarray/indexes/range_index.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/indexes/range_index.py b/xarray/indexes/range_index.py index 4f8d52f6510..37a0e8a51d9 100644 --- a/xarray/indexes/range_index.py +++ b/xarray/indexes/range_index.py @@ -347,7 +347,9 @@ def linspace( if coord_name is None: coord_name = dim - if endpoint: + if num == 1: + stop = start + elif endpoint: stop += (stop - start) / (num - 1) transform = RangeCoordinateTransform(