Skip to content

Fix RangeIndex.linspace with num=1#11401

Open
sanand0 wants to merge 2 commits into
pydata:mainfrom
sanand0:codex/range-index-linspace-num-1
Open

Fix RangeIndex.linspace with num=1#11401
sanand0 wants to merge 2 commits into
pydata:mainfrom
sanand0:codex/range-index-linspace-num-1

Conversation

@sanand0

@sanand0 sanand0 commented Jun 22, 2026

Copy link
Copy Markdown

Description

RangeIndex.linspace handles num=1 like numpy.linspace

Checklist

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
Codex Prompts
codex --yolo --model gpt-5.5 --config model_reasoning_effort=medium
I've cloned https://github.com/pydata/xarray/ here. Fix https://github.com/pydata/xarray/issues/11397 - i.e.
MINIMALLY and ELEGANTLY modify `xarray/indexes/range_index.py` as a small maintainer-friendly change so that

- it doesn't increase the number of code lines and
- works when num = 1, matching the NumPy behaviour, i.e. `np.linspace(0, 1, num=1)` returns `array([0.])`

I believe adding `and num > 1` to the `if` condition in `xarray/indexes/range_index.py` would fix this.

```python
if endpoint and num > 1:
    stop += (stop - start) / (num - 1)
```

Begin by writing a regression test like the below (but write it consistent with the xarray test style) to verify that the bug is fixed:

```python
def test_range_index_linspace_num_1():
    index = RangeIndex.linspace(0.0, 1.0, num=1, dim="x")
    assert_array_equal(index.transform.generate_coords()["x"], np.array([0.0]))

def test_range_index_linspace_num_1_endpoint_false():
    index = RangeIndex.linspace(0.0, 1.0, num=1, endpoint=False, dim="x")
    assert_array_equal(index.transform.generate_coords()["x"], np.array([0.0]))
```

Run and test, make sure the tests fail (before the fix) only because of the bug and pass after the fix.

---

Is it possible to fold the test into an existing test function so that the test code becomes more elegant and maintainable? If so, do that.

---

Is further simplification possible AND desirable?

@welcome

welcome Bot commented Jun 22, 2026

Copy link
Copy Markdown

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should RangeIndex.linspace handle num=1 like numpy.linspace?

1 participant